Soorya Prakash
Soorya Prakash

Reputation: 991

How to read and parse the json file and add it into the shell script variable?

I am having the file named loaded.json which contains the below json data.

    {
        "name" : "xat",
        "code" : "QpiAc"
    }
    {
        "name" : "gbd",
        "code" : "gDSo3"
    }
    {
        "name" : "mbB",
        "code" : "mg33y"
    }
    {
        "name" : "sbd",
        "code" : "2Vl1w"
    }

Form the shell script i need to read and parse the json and add the result to the variable and print it like this.

#!/bin/sh
databasename = cat loaded.json | json select '.name'
echo $databasename

When i run the above script i am getting error like

databasename command not found
json command not found

I am new to shell script please help me to solve this problem

Upvotes: 13

Views: 63367

Answers (2)

Arnab Nandy
Arnab Nandy

Reputation: 6702

Replace this,

databasename=`cat loaded.json | json select '.name'`

or try jq command,

databasename=`jq '.name' loaded.json`

For more information read this article.

Upvotes: 24

Soorya Prakash
Soorya Prakash

Reputation: 991

I can able to get the result using jq command like below

databasename=`cat loaded.json | jq '.name'`

Upvotes: 4

Related Questions