24363

how to parse json where key is variable in python?

Question:

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

<strong>i4x-IITB-CS101-problem-33e4aac93dc84f368c93b1d08fa984fc_2_1</strong> 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 <strong>i4x-IITB-CS101-problem-33e4aac93dc84f368c93b1d08fa984fc_2_1</strong> key and

data inside this key ??

Answer1:

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)

Answer2:

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.

Answer3:

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.

Recommend

  • Facebook Oauth CORS error
  • How to programmatically open login window using GreaseMonkey userscript for implicit grant authoriza
  • Unable to pass FB OAuth for my application
  • Google API: Authenticate request with OAuth but without user consent window then request Google data
  • Difference between Java Post Request and browser post request
  • Authenticating to BigQuery REST API via OAuth2 from Node.js
  • onDraw() Method work only when i creating my object in the onDraw()
  • How can i attach an listener to listview?
  • list view image is getting changed when scrolling
  • Android: ListAdapter example redraws same content
  • Fragment tabhost is not working in Fragment
  • Why setOnCheckedChangeListener not working in a popup that uses LayoutInflater?
  • Display Two Fragments at the same time
  • How to Filter ListAdapter using getFilter() within a fragment
  • Set button height equal to the button width
  • Grid-lines on a GridView
  • Toggle button in a list view loose their state when scrolled of screen in Android
  • Execute SQL Server stored procedure from VBA and retrieve all messages and result sets
  • layout-lt-mdhas been deprecated. Please use a `layout-gt-` variant
  • Product and product template in Odoo 10
  • SearchView wrong alignment
  • List view crashes when scrolling “The application may be doing too much work on its main thread.”
  • Listview with multiple strings
  • Spree, Ruby on Rails - Add to cart multiple variants of the same product
  • Shopify API CARTS - Changing line_item line_price for price Override
  • Not able to display correct data in table -AngularJS
  • Activity overlaying. WindowManager.LayoutParams
  • Syntax error on tokens, AnnotationName expected instead - error on query
  • Implicit joins and Where in Doctrine - how?
  • I18n locale disregarding fallbacks
  • converting text file into xml using php?
  • Blackberry - Custom EditField Cursor
  • Content-Length header not returned from Pylons response
  • Ajax Loaded meta Tags
  • Xamarin Forms - UWP Fonts
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Jquery - Jquery Wysiwyg return html as a string
  • Arrays break string types in Julia
  • WPF Applying a trigger on binding failure
  • Java static initializers and reflection