liveBook · Manning

JSON

As explained in Functional Programming in Scala, Second Edition

JSON is a text-based data interchange format for representing structured values such as objects, arrays, strings, numbers, booleans, and null.

JSON is a text-based format for structured values. An object contains string keys and values; values may be objects, arrays, strings, numbers, booleans, or null. A parser can first turn a document into a syntax tree without performing further processing. section 9.4.1

A JSON document

A market-data object can contain strings such as "MSFT", booleans such as true, numbers such as 30.66 and 8.38e9, and an array of related company tickers. The corresponding Scala representation uses cases such as JString, JNumber, JBool, JArray, JObject, and JNull. section 9.4.1

Build a JSON parser by composing small parsers and preserving the document’s structure:

  • Parse a document as optional leading whitespace followed by an array or object, then require end of input. section 9.8
  • Parse arrays as bracketed, comma-separated values and convert the results to JArray. section 9.8
  • Parse objects as comma-separated key-value pairs and convert them to JObject. section 9.8
  • Parse literals such as null, numbers, quoted strings, true, and false, using token parsers and regular expressions where useful. section 9.8
  • Use map, map2, flatMap, many, sep, and choice to combine parsers while discarding separators and retaining meaningful results. section 9.4.2
{
  "Company name" : "Microsoft Corporation",
  "Ticker"  : "MSFT",
  "Active"  : true,
  "Price"   : 30.66,
  "Shares outstanding" : 8.38e9,
  "Related companies" :
    [ "HPQ", "IBM", "YHOO", "DELL", "GOOG" ]
}

Inspect a complete JSON example containing nested objects, arrays, strings, booleans, and numbers. section 9.4.1

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Defines JSON values and works through a full parser, stopping at syntax-tree construction rather than further document processing.
Examples
Uses a Microsoft market-data document and implementation exercises for arrays, objects, literals, separators, whitespace, and end-of-input.
What it assumes
Assumes familiarity with parser primitives such as string, regex, succeed, flatMap, choice, map, map2, many, and many1.
What you can do after
Builds a JSON parser from scratch with parser primitives, combinators, recursive grammar rules, and a typed JSON representation.
How it connects
Connects JSON to parser combinators, regular expressions, recursive values, arrays, objects, literals, and error handling.

Also explained in

Compare how 35 books explain JSON →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in Scala, Second Edition. The section links point to the parts of the book it draws on.