Hacker Public Radio

HPR3252: Simple JSON querying tool (also YAML, and to a lesser extent XML)


Listen Later

JSON
Json is a cool little data serialization language, that allows you to easily and clearly demarcate blocks of data by nesting data structures such as lists (enclosed by square brackets) and key-value pairs or "dictionaries" (enclosed by curly braces). So that in the end you get something that looks like this
{
"first list" : [ "element1", "element2", {"element3" : "is another k-v pair", "but contains" : ["a" , "list", "of", "words"]}] ,
"this value is a string" : "1" ,
"and this is a number" : 23 ,
"and floating point" : 1.413
}
Aside from:
Lists are enclosed in [] and each element is separated by ,
Key-value pair lists are enclosed in {} and have the key and value separated by : and each pair is separated by ,
Keys have to strings quoted with double quotes
Numbers may be left unquoted (but just in value fields)
There are no restrictions to what you can do with JSON. Given how explicit the syntax is then, it makes for very easy parsing, and there are plenty of good parser out there. My favourite JSON parser is jq(1).
A canonical representation of the JSON example above can easily be obtained with jq by simply calling jq '' file.json (or piping the file through stdin, or even putting the contents properly quoted as the second argument).
{
"first list": [
"element1",
"element2",
{
"element3": "is another k-v pair",
"but contains": [
"a",
"list",
"of",
"words"
]
}
],
"this value is a string": "1",
"and this is a number": 23,
"and floating point": 1.413
}
You can also use jq in a shell script to obtain, for example the second element of the first list:
$ jq '."first list"[1]' example.json
"element2"
So to get the value associated to a key you use the notation .key and to get the k-th element you use the notation [k-1]. To remove the quotes on the string you can use the -r flag which stands for raw output.
jq(1) also gives you a few more functionalities that can be useful like getting the number of elements in a list with the length function.
$ jq 'length' example.json
3
$ jq '."first list"[2]."but contains" | length'
4
Another useful feature is getting the list of keys from a key-value pair list which can be done with the function keys
$ jq '."first list"[2] | keys[]' example.json
"but contains",
"element3"
The query language is much much more flexible than this, but for most cases this should be enough for simple configuration querying.
YAML and XML??
The yq project allows one to use the exact same syntax as jq to query, and emit (and therefore also transcode) yaml and XML, extending the usefulness of the query language.
So for example looking at the previous file through yq gives:
$ yq -y '' example.json
first list:
- element1
- element2
- element3: is another k-v pair
but contains:
- a
- list
- of
- words
this value is a string: '1'
and this is a number: 23
and floating point: 1.413
And the output of this can be of course queried with yq itself, or can be used
...more
View all episodesView all episodes
Download on the App Store

Hacker Public RadioBy Hacker Public Radio

  • 4.2
  • 4.2
  • 4.2
  • 4.2
  • 4.2

4.2

34 ratings


More shows like Hacker Public Radio

View all
The Changelog: Software Development, Open Source by Changelog Media

The Changelog: Software Development, Open Source

290 Listeners

Defensive Security Podcast - Malware, Hacking, Cyber Security & Infosec by Jerry Bell and Andrew Kalat

Defensive Security Podcast - Malware, Hacking, Cyber Security & Infosec

372 Listeners

LINUX Unplugged by Jupiter Broadcasting

LINUX Unplugged

268 Listeners

SANS Internet Stormcenter Daily Cyber Security Podcast (Stormcast) by Johannes B. Ullrich

SANS Internet Stormcenter Daily Cyber Security Podcast (Stormcast)

651 Listeners

Curious Cases by BBC Radio 4

Curious Cases

820 Listeners

The Strong Towns Podcast by Strong Towns

The Strong Towns Podcast

422 Listeners

Late Night Linux by The Late Night Linux Family

Late Night Linux

164 Listeners

Darknet Diaries by Jack Rhysider

Darknet Diaries

8,061 Listeners

Cybersecurity Today by Jim Love

Cybersecurity Today

179 Listeners

CISO Series Podcast by David Spark, Mike Johnson, and Andy Ellis

CISO Series Podcast

189 Listeners

TechCrunch Daily Crunch by TechCrunch

TechCrunch Daily Crunch

42 Listeners

Strict Scrutiny by Crooked Media

Strict Scrutiny

5,794 Listeners

2.5 Admins by The Late Night Linux Family

2.5 Admins

98 Listeners

Cyber Security Headlines by CISO Series

Cyber Security Headlines

139 Listeners

What the Hack? by DeleteMe

What the Hack?

228 Listeners