Chapter 16. Processing JSON Data
16.0 Introduction
JSON, or JavaScript Object Notation, is all of the following:
-
A simple, lightweight data interchange format
-
A simpler, lighter alternative to XML
-
Easy to generate with
println()or with one of several APIs -
Recognized directly by the JavaScript parser in all browsers, server-side node implementations, etc.
-
Supported with add-on frameworks for all common languages (Java, C/C++, Perl, Ruby, Python, Lua, Erlang, Haskell, to name a few); a ridiculously long list of supported languages (including two dozen parsers for Java alone) is on the JSON home page
A basic JSON message might look like Example 16-1.
Example 16-1. json/src/main/resources/json/softwareinfo.json
{"name":"robinparse","version":"1.2.3","description":"Another Parser for JSON","className":"RobinParse","contributors":["Robin Smythe","Jon Jenz","Jan Ardann"]}
As you can see, the syntax is simple, nestable, and amenable to human inspection.
The JSON home page provides a concise summary of JSON syntax.
There are two kinds of structure: JSON objects (maps) and JSON arrays (lists).
JSON objects are sets of name and value pairs, which can be represented in Java
either as a java.util.Map or as the properties of
a Java object. For example, the fields of a LocalDate (see Recipe 6.1) object
for April 1, 2025, might be represented like this:
{"year":2025,"month":4,"day":1}
JSON arrays are ordered lists, represented in Java either as ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access