nestedData = """{"dashboardId":8,"evalMatches":[{"value":1,"metric":"newdb.count { creation_date: 2020-11-26 10:29:13 indicator_label: BBP 1H market: ORN/USDT }","tags":{"creation_date":"2020-11-26 10:29:13","indicator_label":"BBP 1H","market":"ORN/USDT"}},{"value":1,"metric":"newdb.count { creation_date: 2020-11-26 10:29:13 indicator_label: STOCHRSI 1H market: UTK/BTC }","tags":{"creation_date":"2020-11-26 10:29:13","indicator_label":"STOCHRSI 1H","market":"UTK/BTC"}},{"value":1,"metric":"newdb.count { creation_date: 2020-11-26 10:29:13 indicator_label: STOCHRSI 4H market: ORN/USDT }","tags":{"creation_date":"2020-11-26 10:29:13","indicator_label":"STOCHRSI 4H","market":"ORN/USDT"}}],"orgId":1,"panelId":14,"ruleId":52,"ruleName":"test alert","ruleUrl":"http://localhost:3000/d/1xBsMrIMk/alert?tab=alert\u0026editPanel=14\u0026orgId=1","state":"alerting","tags":{},"title":"[Alerting] test alert"}"""

मैं प्रासंगिक "बाजार" और "संकेतक_लेबल" निकालने का एक तरीका ढूंढ रहा हूं। मैंने इस कोड का उपयोग किया है लेकिन यह केवल एक निष्कर्षण चरण करता है:

json_obj = json.loads(cts)
market = (json_obj["evalMatches"][0]["tags"]["market"])
indicator_label = (json_obj["evalMatches"][1]["tags"]["indicator_label"])
creation_date = (json_obj["evalMatches"][0]["tags"]["creation_date"])

मुझे इस तरह का परिणाम चाहिए

market='ORN/USDT',indicator_label'BBP 1H,STOCHRSI 4H'
market='UTK/USDT',indicator_label'STOCHRSI 1H'
-1
navid 26 नवम्बर 2020, 11:37

1 उत्तर

सबसे बढ़िया उत्तर

तुम कोशिश कर सकते हो:

import json
nestedData = """{"dashboardId":8,"evalMatches":[{"value":1,"metric":"newdb.count { creation_date: 2020-11-26 10:29:13 indicator_label: BBP 1H market: ORN/USDT }","tags":{"creation_date":"2020-11-26 10:29:13","indicator_label":"BBP 1H","market":"ORN/USDT"}},{"value":1,"metric":"newdb.count { creation_date: 2020-11-26 10:29:13 indicator_label: STOCHRSI 1H market: UTK/BTC }","tags":{"creation_date":"2020-11-26 10:29:13","indicator_label":"STOCHRSI 1H","market":"UTK/BTC"}},{"value":1,"metric":"newdb.count { creation_date: 2020-11-26 10:29:13 indicator_label: STOCHRSI 4H market: ORN/USDT }","tags":{"creation_date":"2020-11-26 10:29:13","indicator_label":"STOCHRSI 4H","market":"ORN/USDT"}}],"orgId":1,"panelId":14,"ruleId":52,"ruleName":"test alert","ruleUrl":"http://localhost:3000/d/1xBsMrIMk/alert?tab=alert\u0026editPanel=14\u0026orgId=1","state":"alerting","tags":{},"title":"[Alerting] test alert"}"""
data = json.loads(nestedData)

output = {}
for val in data["evalMatches"]:
    if val["tags"]["market"] not in output:
        output[val["tags"]["market"]] = []
    output[val["tags"]["market"]].append(str(val["tags"]["indicator_label"]))

for data in output:
    print("market='" + data + "',indicator_label'" + ",".join(output[data]) + "'" )

आउटपुट:

market='ORN/USDT',indicator_label'BBP 1H,STOCHRSI 4H'
market='UTK/BTC',indicator_label'STOCHRSI 1H'
0
Harsha Biyani 26 नवम्बर 2020, 11:51