how to parse json where key is variable in python?

二次信任 提交于 2019-12-12 02:52:21

问题


i am parsing a log file which is in json format, and contains data in the form of key : value pair.

i was stuck at place where key itself is variable. please look at the attached code

in this code i am able to access keys like username,event_type,ip etc.

problem for me is to access the values inside the "submission" key where

i4x-IITB-CS101-problem-33e4aac93dc84f368c93b1d08fa984fc_2_1 is a variable key which will change for different users,

how can i access it as a variable ?

{
    "username": "batista",        
    "event_type": "problem_check",      
    "ip": "127.0.0.1",
    "event": {
        "submission": {
            "i4x-IITB-CS101-problem-33e4aac93dc84f368c93b1d08fa984fc_2_1": {
                "input_type": "choicegroup",
                "question": "",
                "response_type": "multiplechoiceresponse",
                "answer": "MenuInflater.inflate()",
                "variant": "",
                "correct": true
            }
        },
        "success": "correct",
        "grade": 1,
        "correct_map": {
            "i4x-IITB-CS101-problem-33e4aac93dc84f368c93b1d08fa984fc_2_1": {
                "hint": "",
                "hintmode": null,
                "correctness": "correct",
                "npoints": null,
                "msg": "",
                "queuestate": null
            }
        }

this is my code how i am solving it :

import json
import pprint
with open("log.log") as infile:
# Loop until we have parsed all the lines.
for line in infile:
    # Read lines until we find a complete object
    while (True):
        try:
            json_data = json.loads(line)

            username = json_data['username']
            print "username :- " + username

        except ValueError:                
            line += next(infile)

how can i access i4x-IITB-CS101-problem-33e4aac93dc84f368c93b1d08fa984fc_2_1 key and

data inside this key ??


回答1:


You don't need to know the key in advance, you can simply iterate over the dictionary:

for k,v in obj['event']['submission'].iteritems():
   print(k,v)



回答2:


Suppose you have a dictionary of type d = {"a":"b"} then d.popitem() would give you a tuple ("a","b") which is (key,value). So using this you can access key-value pairs without knowing the key.

In you case if j is the main dictionary then j["event"]["submission"].popitem() would give you tuple

("i4x-IITB-CS101-problem-33e4aac93dc84f368c93b1d08fa984fc_2_1": {
                "input_type": "choicegroup",
                "question": "",
                "response_type": "multiplechoiceresponse",
                "answer": "MenuInflater.inflate()",
                "variant": "",
                "correct": true
            })

Hope this is what you were asking.




回答3:


using python json module you'll end up with a dictionary of parsed values from the above JSON data

 import json
 parsed = json.loads(this_sample_data_in_question)
 # parsed is a dictionary, so are "correct_map" and "submission" dictionary keys within "event" key 

So you could iterate over the key, values of the data as a normal dictionary, say like this:

 for k, v in parsed.items():
     print k, v

Now you could find the (possible different values) of "i4x-IITB-CS101-problem-33e4aac93dc84f368c93b1d08fa984fc_2_1" key in a quick way like this:

import json

parsed = json.loads(the_data_in_question_as_string)
event = parsed['event']

for key, val in event.items():
    if key in ('correct_map', 'submission'):
        section = event[key]
        for possible_variable_key, its_value in section.items():
            print possible_variable_key, its_value

Of course there might be better way of iterating over the dictionary, but that one you could choose based on your coding taste, or performance if you have a fairly larger kind of data than the one posted in here.



来源:https://stackoverflow.com/questions/25970436/how-to-parse-json-where-key-is-variable-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!