#487 · Parsing · Expert · 260 XP · about 60 min
JSON is the text format most web services use to send data. Write parse_json(text) that turns a JSON document into Python values without using the json module, and raises ValueError if the text is not valid JSON.
The values, and what they become:
null, true and false become None, True and False.int when it has no fraction and no exponent, and a float otherwise. Convert the number's text with int() or float(), so 2.5e3 gives 2500.0.str.[ then values separated by commas then ], becomes a list.{ then "key": value pairs separated by commas then }, becomes a dict with the keys in the order they appear. If a key appears twice, the later value wins and the key keeps its first place.The strict rules. Breaking any of them makes the text invalid:
-, then 0 or a digit 1 to 9 followed by more digits, then optionally . and one or more digits, then optionally e or E, an optional + or -, and one or more digits. So 01, 1., .5, +1 and 1e are all invalid.\", \\, \/, \b, \f, \n, \r, \t, or \u followed by exactly four hex digits. A \u escape for a high surrogate (D800 to DBFF) followed by a \u escape for a low surrogate (DC00 to DFFF) makes one character, as in "😀".\n and \r only) may appear before and after any value, comma or colon. The whole text must be one value, with nothing else after it.parse_json('{"name": "Ada", "age": 36, "langs": ["en", "fr"]}') # returns {'name': 'Ada', 'age': 36, 'langs': ['en', 'fr']} parse_json('[1, -2.5, 3e2, true, false, null]') # returns [1, -2.5, 300.0, True, False, None] parse_json('"tab\\there \\u00e9"') # returns 'tab\there é' parse_json('[1, 2,]') # raises ValueError
text has at most 500,000 characters.First solves only, ranked by stars and then time.