7

I'm in over my head, trying to parse JSON for my first time and dealing with a multi dimensional array.

{
  "secret": "[Hidden]",
  "minutes": 20,
  "link": "http:\/\/www.1.com",
  "bookmark_collection": {
    "free_link": {
      "name": "#free_link#",
      "bookmarks": [
        {
          "name": "1",
          "link": "http:\/\/www.1.com"
        },
        {
          "name": "2",
          "link": "http:\/\/2.dk"
        },
        {
          "name": "3",
          "link": "http:\/\/www.3.in"
        }
      ]
    },
    "boarding_pass": {
      "name": "Boarding Pass",
      "bookmarks": [
        {
          "name": "1",
          "link": "http:\/\/www.1.com\/"
        },
        {
          "name": "2",
          "link": "http:\/\/www.2.com\/"
        },
        {
          "name": "3",
          "link": "http:\/\/www.3.hk"
        }
      ]
    },
    "sublinks": {
      "name": "sublinks",
      "link": [
        "http:\/\/www.1.com",
        "http:\/\/www.2.com",
        "http:\/\/www.3.com"
      ]
    }
  }
}

This is divided into 3 parts, the static data on my first dimension (secret, minutes, link) Which i need to get as seperate strings.

Then I need a dictionary per "bookmark collection" which does not have fixed names, so I need the name of them and the links/names of each bookmark.

Then there is the seperate sublinks which is always the same, where I need all the links in a seperate dictionary.

I'm reading about parsing JSON but most of the stuff I find is a simple array put into 1 dictionary. Does anyone have any good techniques to do this ?

Alex R
  • 637
  • 1
  • 8
  • 20
  • http://docs.python.org/tutorial/datastructures.html – Ignacio Vazquez-Abrams Sep 10 '12 at 01:16
  • I don't think you are using the word "array" properly here. By array, I think "most people" mean a - possibly multidimensional - data structure composed of elements all of the same type. JSON, and their Python couterpart "arbitrarily nested dicts and lists" are usually not arrays, but "objects", that's the whole point of it: something to "hold" an arbitrary structure of arbitrary objects (objects must be of json-serializeable types). – heltonbiker Sep 10 '12 at 02:07
  • yes, that might also be why my googling failed miserably .. – Alex R Sep 10 '12 at 08:06

2 Answers2

13

After you parse the JSON, you will end up with a Python dict. So, suppose the above JSON is in a string named input_data:

import json
# This converts from JSON to a python dict
parsed_input = json.loads(input_data)

# Now, all of your static variables are referenceable as keys:
secret = parsed_input['secret']
minutes = parsed_input['minutes']
link = parsed_input['link']

# Plus, you can get your bookmark collection as:
bookmark_collection = parsed_input['bookmark_collection']

# Print a list of names of the bookmark collections...
print bookmark_collection.keys() # Note this contains sublinks, so remove it if needed

# Get the name of the Boarding Pass bookmark:
print bookmark_collection['boarding_pass']['name']

# Print out a list of all bookmark links as:
#  Boarding Pass
#    * 1: http://www.1.com/
#    * 2: http://www.2.com/
#  ...
for bookmark_definition in bookmark_collection.values():
    # Skip sublinks...
    if bookmark_definition['name'] == 'sublinks':
        continue
    print bookmark_definition['name']
    for bookmark in bookmark_definition['bookmarks']:
        print "    * %(name)s: %(link)s" % bookmark

# Get the sublink definition:
sublinks = parsed_input['bookmark_collection']['sublinks']

# .. and print them
print sublinks['name']
for link in sublinks['link']:
    print '  *', link
jcater
  • 501
  • 4
  • 7
  • What is remarkable about JSON is that its syntax is EXACTELY the same you would use to create the same thing in Python, considering "thing" as being arbitrarily nested dicts and lists. – heltonbiker Sep 10 '12 at 01:42
  • @heltonbiker: [Except the exceptions](http://stackoverflow.com/questions/6627635/is-json-syntax-a-strict-subset-of-python-syntax), of course. – Ignacio Vazquez-Abrams Sep 10 '12 at 01:45
  • wauv, you immediately made me understand how it all works. thankyou! Why is this so hard to find an example of on the net... – Alex R Sep 10 '12 at 01:57
  • @IgnacioVazquez-Abrams I guess my "exactely" should have been put between quotes, not in capitals... ;o) – heltonbiker Sep 10 '12 at 02:02
2

Hmm, doesn't json.loads do the trick?

For example, if your data is in a file,

import json
text = open('/tmp/mydata.json').read()

d = json.loads(text)

# first level fields
print d['minutes'] # or 'secret' or 'link'

# the names of each of bookmark_collections's items
print d['bookmark_collection'].keys()

# the sublinks section, as a dict
print d['bookmark_collection']['sublinks']

The output of this code (given your sample input above) is:

20
[u'sublinks', u'free_link', u'boarding_pass']
{u'link': [u'http://www.1.com', u'http://www.2.com', u'http://www.3.com'], u'name': u'sublinks'}

Which, I think, gets you what you need?

ron rothman
  • 17,348
  • 7
  • 41
  • 43