liveBook · Manning

JSON.parse

As explained in Data-Oriented Programming: Reduce software complexity

JSON.parse is the standard JavaScript function that parses a JSON string into the corresponding JavaScript value.

When a function returns JSON text, comparing raw strings is a trap: two strings can serialize the same data in different property orders. Parse the text first, then test the resulting data. section 6.3

JSON.parse deserializes a JSON string into a JavaScript data structure, commonly a map. That turns an encoded response back into data that tests and application code can inspect. section 6.3 section 11.3

Testing a JSON-returning function

Parse Library.searchBooksByTitleJSON's result and compare it with the expected array using _.isEqual. A search for "Watchmen" should produce bookInfo; a search for "Batman" should produce [].

Reading a parsed request

var myString = "{\"publicationYear\":1987,\"title\":\"Watchmen\"}"; var myData = JSON.parse(myString); _.get(myData, "title"); // "Watchmen"

Listing 11.2 Parsing a JSON string in JavaScript
var jsonString = 
  '{"title":"habit","fields":["title","weight","number_of_pages"]}';
JSON.parse(jsonString);

Inspect a minimal JavaScript request-parsing example before comparing the equivalent Java Gson call. section 11.3

How Data-Oriented Programming: Reduce software complexity covers it

How deep it goes
Demonstrates string-to-map conversion and field access, but does not develop parser internals or JSON validation.
Examples
Uses book-search tests and a search-request payload containing title and fields.
What it assumes
Assumes familiarity with JSON strings and JavaScript values while demonstrating deserialization from zero.
What you can do after
Apply it via native JavaScript to inspect parsed payloads and compare structured JSON results.
How it connects
Connects JSON.parse with JSON.stringify, serialization, unit testing, maps, web server frameworks, and JSON schema.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming: Reduce software complexity. The section links point to the parts of the book it draws on.