0

I am new to shell scripting,
I want to read a json variable's attribute values,
I have a sample json like -

{
    "userId": 1,
    "name": "jhonny",
    "cities": [{
        "id": 1,
        "name": "KL"
    }],
    "otherinfo": {
        "hobby": "reading",
        "nickname": "john"
    }
}

I tried the following script -

#!/bin/bash

temp='{"userId":1,"name":"jhonny","cities":[{"id":1,"name":"KL"}],"otherinfo":{"hobby":"reading","nickname":"john"}}'

echo $temp | jq .name

I get a test.sh: line 4: jq: command not found I am following this -
Read JSON variable in Shell Script

Can someone help how I could extract cities[0].name and otherinfo.nickname from the shell variable?

Dev1ce
  • 5,390
  • 17
  • 90
  • 150
  • 3
    Did you install jq? https://stedolan.github.io/jq/ – stephanmg Nov 05 '19 at 16:32
  • Is it possible with any installation? I just tried that answer but didn’t know it required installation – Dev1ce Nov 05 '19 at 16:34
  • I don't know. Can you please check the website? I learned today that this is sed for JSON data basically, but is not a standard tool of course which will be pre-installed with your OS. – stephanmg Nov 05 '19 at 16:39
  • `jq` uses `yq`, so you will also need to install that too. – Darren Smith Nov 05 '19 at 16:46
  • Any pure bash/shell solution? Want to avoid installations :/ – Dev1ce Nov 05 '19 at 16:48
  • 2
    If you're working with json, you need a tool that understands the format, like jq. – Shawn Nov 05 '19 at 16:54
  • Sure, but this will involve reinventing the wheel. But sure you can. Look into sed/awk. – stephanmg Nov 05 '19 at 17:33
  • What OS are you using? – Mark Setchell Nov 05 '19 at 20:27
  • Note that the duplicate I closed this with includes answers that use Python rather than jq. Any answer that *doesn't* use a tool (like Python or jq) with a conformant JSON parser should be avoided. – Charles Duffy Nov 05 '19 at 21:46
  • 1
    @stephanmg, ...please don't encourage folks -- reinventions of that wheel pretty much always end up getting the corner cases wrong, thus resulting in brittle systems that fail whenever data changes in unanticipated ways. [The canonical response to folks trying to do regex-based "parsing" of HTML](https://stackoverflow.com/a/1732454/14122) comes to mind. – Charles Duffy Nov 05 '19 at 21:49
  • @CharlesDuffy: Yes, you are right. It is of course not advised to parse HTML with regex. I'm sorry I did not want to mislead somebody. – stephanmg Nov 06 '19 at 14:15

1 Answers1

0

The JSON module is a standard part of Python3, so if you don't want to install jq, you could maybe use Python:

python3 -c 'import json,sys;JSON=json.load(sys.stdin);print(JSON["cities"][0]["name"]);print(JSON["otherinfo"]["nickname"]);' < data.json

Sample Output

KL
john

That assumes your JSON data is in a file called data.json. If it's in a shell variable called JSON, omit the < data.json from the end and use:

echo "$JSON" | python3 -c ...
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432