Fancy stuff: JSON

Abhishek Jain
Better Engineer
Published in
4 min readFeb 10, 2020
Photo by Marvin Meyer on Unsplash

Let’s take our skills to the next level.

I will put the working knowledge of these languages/tools here and not whole lot of details. After all, we are hardware engineers and not a full time web developer. So we can set the bar low and still get things done in short time.

JSON

From w3schools:

JSON: JavaScript Object Notation.

JSON is a syntax for storing and exchanging data.

JSON is text, written with JavaScript object notation.

Think of it like a data structure which is easy to share over the web. But it also doesn’t really have to be only web. You can use it locally in your tools too.

A simple json file looks like this:

{"menu": {
"id": "file",
"value": "some_value",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}

source https://json.org/example.html

Basically it is huge hash table with a lot of nested hash tables and arrays etc.

You can then look up exact syntax on the above websites when you want to develop your own json file.

So why do you need to know this?

If you want to develop a solution which can be scaled and reused with other flows, you need to use a standard format to store and transfer data.

XML is another such format, but it is little difficult to read.

How to read a json file in python?

You can use the json package to read the json file in a dictionary for easy use.

We will read the example json file we mentioned above.

import jsonjson_file = "my.json"j = {} # our dictionary to store the json filewith open (json_file) as f:
j = json.load(f)
print(j["menu"]["value"])

prints “some_value”

How to read a json file in JavaScript

This code snippet may not work for cross domain traffic. So be aware of that part.

Assume we are going to get the example json file as a response to our request.

We can access the “id” value like this.

Read json in Bash

You can parse json files in Bash using “jq”

To simply read our sample file above

>>jq . test.json
{
"menu": {
"id": "file",
"value": "some_value",
"popup": {
"menuitem": [
{
"value": "New",
"onclick": "CreateNewDoc()"
},
{
"value": "Open",
"onclick": "OpenDoc()"
},
{
"value": "Close",
"onclick": "CloseDoc()"
}
]
}
}
}

To get the value stored in “id”

>>jq .menu.id test.json
"file"

To get rid of those quotes, we can use -r

>>jq -r .menu.id test.json
file

So far so good.

If we want to access elements from an array, we just need to tweak the command a little bit. In our example, menuitem is an array.

To print all elements:

>>jq '.menu.popup.menuitem | .[]' test.json
{
"value": "New",
"onclick": "CreateNewDoc()"
}
{
"value": "Open",
"onclick": "OpenDoc()"
}
{
"value": "Close",
"onclick": "CloseDoc()"
}

To access element 0

>>jq '.menu.popup.menuitem| .[0]' test.json
{
"value": "New",
"onclick": "CreateNewDoc()"
}

To access “value” of element 0

>>jq -r '.menu.popup.menuitem| .[0].value' test.json
New

If we don’t put any index, then it will print all the “value” elements

>>jq -r '.menu.popup.menuitem| .[].value' test.json
New
Open
Close

Now let’s say we don’t know the index of the element we want to access, but we know the value. Let’s say we want to reach to element “OpenDoc()”. We can do it like this

>>jq -r '.menu.popup.menuitem[] | select(."value" == "Open")' test.json
{
"value": "Open",
"onclick": "OpenDoc()"
}

We first get all the elements of the menuitem array, then pipe it and use select on it, that gives us both elements of that nested array.

Now we want to get just the “onclick”. We can pipe it again

>>jq -r '.menu.popup.menuitem[] | select(."value" == "Open") | ."onclick"' test.json
OpenDoc()

Some good jq tutorials

Write your own json file and check for syntax

With some help from w3schools tutorial, you should be able to write your own json file. But how do you check if your syntax is correct? because if it is not, then your python script won’t be able to read it.

We can check the syntax with this command:

python -m json.tool < my.json > /dev/null

This will check “my.json” file for errors and print the if there are any.

--

--