json_bnf = """ object { members } {} members string : value members , string : value array [ elements ] [] elements value elements , value value string number object array true false null """ from pyparsing import * TRUE = Keyword("true") FALSE = Keyword("false") NULL = Keyword("null") jsonString = dblQuotedString.setParseAction( removeQuotes ) jsonNumber = Combine( Optional('-') + ( '0' | Word('123456789',nums) ) + Optional( '.' + Word(nums) ) + Optional( Word('eE',exact=1) + Word(nums+'+-',nums) ) ) jsonObject = Forward() jsonValue = Forward() jsonElements = delimitedList( jsonValue ) jsonArray = Group( Suppress('[') + jsonElements + Suppress(']') ) jsonValue << ( jsonString | jsonNumber | jsonObject | jsonArray | TRUE | FALSE | NULL ) memberDef = Group( jsonString + Suppress(':') + jsonValue ) jsonMembers = delimitedList( memberDef ) jsonObject << Dict( Suppress('{') + jsonMembers + Suppress('}') ) jsonComment = cppStyleComment jsonObject.ignore( jsonComment ) TRUE.setParseAction( replaceWith(True) ) FALSE.setParseAction( replaceWith(False) ) NULL.setParseAction( replaceWith(None) ) jsonString.setParseAction( removeQuotes ) def convertNumbers(s,l,toks): n = toks[0] try: return int(n) except ValueError, ve: return float(n) jsonNumber.setParseAction( convertNumbers ) testdata = """ { "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": [{ "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "TrueValue": true, "FalseValue": false, "Gravity": -9.8, "LargestPrimeLessThan100": 97, "AvogadroNumber": 6.02E23, "EvenPrimesGreaterThan2": null, "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML", "markup"] }] } } } """ import pprint results = jsonObject.parseString(testdata) pprint.pprint( results.asList() ) print def testPrint(x): print type(x),x print results.glossary.GlossDiv.GlossList.keys() testPrint( results.glossary.title ) testPrint( results.glossary.GlossDiv.GlossList.ID ) testPrint( results.glossary.GlossDiv.GlossList.FalseValue ) testPrint( results.glossary.GlossDiv.GlossList.Acronym )