13

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

1
  • 2
    Remove spaces around '=', and wrap command into $(). And you should have json application installed Commented Jan 28, 2015 at 6:25

2 Answers 2

24

Replace this,

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

or try jq command,

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

For more information read this article.

Sign up to request clarification or add additional context in comments.

3 Comments

databasename = cat loaded.json | json select '.name' using this command i am getting error as "json: command not found". if i use underscore insted of json i am getting error as "Error while parsing STDIN in mode 'lax': Unexpected token :". I installed both underscore-cli and also json library.
@SooryaPrakash replace above code of json command with modification to your shellscript first, if not working then try the jq command.
@DrCord link is working perfectly
4

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.