A partial JSON reader: live and stable views
The same reply is read after every 6 characters. The Java version uses the course's Json helper; real Java projects use Jackson.
Half of a JSON document is not valid JSON, so a normal parser fails until the end. A partial-JSON reader closes the open string and brackets so you can show fields as they arrive, for display only, never for decisions.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write close_brackets(text) that, for text with no strings (only {, }, [, ] and other characters), returns the closing brackets needed, in the right order. For {[1,{ it returns }]}.
Write last_complete_prefix(text) that returns the text before the last comma, or an empty string if there is no comma.
Explain it without notes
Why is a prefix of a JSON document usually not valid JSON, and how does a partial reader deal with it?
What is the difference between a live and a stable view, and when do you use each?
Why should you never act on partial JSON?
Expected output
6 chars | live: {} | stable: {}
12 chars | live: {"name":"Ash"} | stable: {}
18 chars | live: {"name":"Asha Rao"} | stable: {}
24 chars | live: {"name":"Asha Rao"} | stable: {"name":"Asha Rao"}
30 chars | live: {"name":"Asha Rao","skills":[""]} | stable: {"name":"Asha Rao"}
36 chars | live: {"name":"Asha Rao","skills":["python"]} | stable: {"name":"Asha Rao"}
42 chars | live: {"name":"Asha Rao","skills":["python","sql"]} | stable: {"name":"Asha Rao"}
48 chars | live: {"name":"Asha Rao","skills":["python","sql"]} | stable: {"name":"Asha Rao","skills":["python","sql"]}
54 chars | live: {"name":"Asha Rao","skills":["python","sql"],"years":1} | stable: {"name":"Asha Rao","skills":["python","sql"]}
56 chars | live: {"name":"Asha Rao","skills":["python","sql"],"years":12} | stable: {"name":"Asha Rao","skills":["python","sql"],"years":12}