Reading JSON

Robert Delwood
2 min readFeb 11, 2020

Using REST is tough enough and you will have to read JSON. But that’s no problem really.

by Robert Delwood

Introduction

REST programming is all about JSON. JSON is an established, well-documented, structured data format that allows exchanging data over the Internet. It’s used extensively in both directions, when data is sent to servers, and when it’s returned from servers. REST applications use it for most response requests, or returning data from a server. Many are uncomfortable with JSON, although there is no reason to be. You need to understand two things about it. It’s a dictionary, and there’s a pattern.

It’s a dictionary

In brief, JSON is data in pairings, like a dictionary entry.

The simplest example is:

"name": "Robert"

The left side of the colon is called the key, and is the name of the entry, here as name. The right side is the value, Robert.

Structures can get exceedingly complex and long, but all entries are ultimately still a pairing. It’s usually the value that is programmers are looking for. This tutorial introduces the concepts and formats of JSON, and explains how to access individual pieces of information programmatically.

Parsing

Getting a value is called parsing.

JSON is returned from requests formatted but as plain text. To be more easily parsed, the JSON needs to be converted to another format. There is a free library called Newtonsoft library designed to handle this. It introduces an object, called JObject, whose important element is essentially JSON in an array. Many users know accessing arrays through an index number, such as myArray[0]. For JObjects, the key is used instead, such as:

jsonObject["name"]

There’s a Pattern

That means we can access fields directly, but notice that getting this information means you have to know something about the structure, mainly the key. In this simple case, you can either look at the source file, there is only one entry after all, or use Visual Studio’s debugger to watch the object.

For small examples, such as they are here, you can simply look at the JSON and figure it out. As demonstrated, you can also use the debugger to walk through the structure. But what if the JSON is more complex than that? There are online JSON editors that you can copy and paste to. Some will “prettify” the code. This is an industry term that formats the structure in a systematic or more human-readable form. Ultimately, there is the object schema. This is the definition of what’s allowed in the JSON. Many APIs require a schema for each JSON object type they use. It validates the JSON, either allowing it to be sent or not. This should be available through the API documentation or the project’s repository, like GitHub. The schema for example 1 would look like this:

{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties":
{
"name": { "type": "string" }
},
"required": [ "count" ]
}

The points of interest are the items under properties. There is one. It is called name and is a string.

Get the Demo

For more examples, or to get a sample Visual Studio project, see the complete Reading JSON.

--

--

Robert Delwood

Programmer/writer/programmer-writer. A former NASA engineer, he ensured astronauts had clean underwear. Yet, it was always about API documentation & automation.