JSON Parser
Updated
Problem statement
Write a JSON parser. The string text holds one JSON value. Read it and return the same value written out in a single fixed form, so that two texts that differ only in spacing, in the order of object keys or in how string characters are escaped give the same answer.
A JSON value is one of these:
- an object:
{, then zero or more members separated by commas, then}. A member is a string key, a colon and a value; - an array:
[, then zero or more values separated by commas, then]; - a string: characters between double quotes, where a backslash starts an escape:
\",\\,\/,\b,\f,\n,\r,\t. An escape can also be\ufollowed by 4 hex digits, which give a character code; - a number: an optional
-, then0or digits that do not start with0, then optionally.and one or more digits, then optionallyeorE, an optional+or-and one or more digits; true,falseornull.
Spaces, tabs, line feeds and carriage returns may appear before and after any value, key, colon or comma, and between the brackets of an empty array or the braces of an empty object, as in [ ] and { }.
Assumption: the fixed form is written like this:
- There is no whitespace anywhere outside strings.
- Array items appear in the same order as in
text. - The members of every object, at every level, are sorted by key. Two keys are compared after their escapes are turned back into characters, character by character, by character code (the character's Unicode value), so
"B"comes before"a". If one key is the start of the other, the shorter key comes first, and the empty key comes before every other key. - A number is copied exactly as it is written in
text, so1.0,-0and2E+3do not change. true,falseandnullare written as they are.- Every string, key or value, is written between double quotes from its characters. A double quote becomes
\"and a backslash becomes\\. The characters with codes 8, 12, 10, 13 and 9 become\b,\f,\n,\rand\t. Every other character whose code is below 32, or is 127 or more, becomes\ufollowed by its code as 4 lowercase hex digits, so the character with code 233 becomes\u00e9. All remaining characters,/included, are written as themselves.
Assumption: text is always valid JSON with nothing but whitespace around its one value. Every character of text is a printable ASCII character (code 32 to 126), a tab, a line feed or a carriage return, and the last three never appear inside a string. A \u escape may use upper or lower case hex digits and never gives a code from D800 to DFFF (hex). No object has two keys that are equal once their escapes are turned back into characters.
Assumption: write the parser yourself; do not use a JSON library from your language.
Function
parseJson(text: String) → StringExamples
Example 1
text = "{ \"zip\": \"02139\", \"Zone\" : [ 3, -0.50, 1E+2 ],\n\t\"id\": {} }"return = "{\"Zone\":[3,-0.50,1E+2],\"id\":{},\"zip\":\"02139\"}"The spaces, the line feed and the tab between tokens are dropped. The keys are sorted by character code: Z (90) comes before i (105), which comes before z (122). The numbers -0.50 and 1E+2 are copied as written, and "02139" is a string, so its leading zero stays. The empty object becomes {}.
Example 2
text = "[\"tab\\there\", \"\\u0041\\u00E9\\/\", \"say \\\"hi\\\"\\\\\"]"return = "[\"tab\\there\",\"A\\u00e9/\",\"say \\\"hi\\\"\\\\\"]"Written without the outer quotes of the input string, text is ["tab\there", "\u0041\u00E9\/", "say \"hi\"\\"]. The tab escape stays \t. \u0041 is the character A, which is written as itself; \u00E9 has code 233, so it is written again as \u00e9 with lowercase hex digits; \/ becomes a plain /. In the last string the quotes and the backslash keep their escapes.
Example 3
text = "{\"\\u0062\": 1, \"a\\n\": 2, \"a\": 3, \"~\": [[], {\"z\": null}]}"return = "{\"a\":3,\"a\\n\":2,\"b\":1,\"~\":[[],{\"z\":null}]}"Written without the outer quotes of the input string, text is {"\u0062": 1, "a\n": 2, "a": 3, "~": [[], {"z": null}]}. The first key decodes to b. The key a is the start of the key a followed by a line feed, so it comes first; the line feed key comes before b because a (97) is smaller than b (98). ~ has code 126, so its member is last. The array keeps its order, and the object inside it is written in the same form.
Constraints
1 <= text.length <= 2000textis valid JSON that follows the input assumption above.- Arrays and objects are nested at most
100levels deep: the outermost array or object is level1, and an array or object directly inside a levelkone is levelk + 1.
More Patreon Phone Screen questions
- Sliding-Window API Rate LimiterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Employee Importance from a DictionaryPHONE SCREEN · Seen Feb 2026
- Uniform Random Number from Coin TossesPHONE SCREEN · Seen Feb 2026
- Wordle Guess CheckerPHONE SCREEN · Seen Jan 2026
- Org Chart Report CountsPHONE SCREEN · Seen Dec 2025
- Format the Missing Numbers from 1 to 99PHONE SCREEN · Seen Sep 2025
- Pythagorean Triplets in an ArrayPHONE SCREEN · Seen Oct 2024
- LRU Cache with TTLPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
- Password GeneratorPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2022
- Merge IntervalsPHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2022
Similar questions at other companies
- Arithmetic Expression EvaluatorMicrosoft · PHONE SCREENMicrosoft interview questions
- Toy Language Generic Return-Type InferenceOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Restricted JSON ParserOld Mission · PHONE SCREEN
- Basic Calculator with Multiplication and ParenthesesBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
- Top K Movies by Play Count from CSV DataTubi · PHONE SCREEN
- Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
- Parse Nested Message TypesApplied Intuition · PHONE SCREENApplied Intuition interview questions
- Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
- Summarize Server Healthcheck LogsNVIDIA · PHONE SCREENNVIDIA interview questions