0
{"status":
    {"reqStatus":"SUCCESS",
            "credentials":"R3DMPF8VIAKG6xLa5vOlp7kqmqE.*AAJTSQACMDIAAlNLABxXVVhKZkJhMmVOL1ZJQWhRUTBYTGY1V2w2TjA9AAR0eXBlAANDVFMAAlMx*****..*",
                    "msgs":[{"msgCode":"ECMSE103",
                            "msgText":"User %A1% was authenticated successfully.",
                            "msgValues":["Tnt_PDU-CD_N53-vPOD4_EO1"]}]
    }
}

I have this json file in my directory and I just want to read this json file and have credentials, I just want to store this key value in variable while running shell script code.

4 Answers 4

2

First, I recommend you format your JSON using a JSON formatter like https://jsonformatter.curiousconcept.com.

Now, let's consider the file test.json which contains your JSON data.

╰─$ cat test.json
{
   "status":{
      "reqStatus":"SUCCESS",

"credentials":"R3DMPF8VIAKG6xLa5vOlp7kqmqE.AAJTSQACMDIAAlNLABxXVVhKZkJhMmVOL1ZJQWhRUTBYTGY1V2w2TjA9AAR0eXBlAANDVFMAAlMx**..*",
      "msgs":[
         {
            "msgCode":"ECMSE103",
            "msgText":"User %A1% was authenticated successfully.",
            "msgValues":[
               "Tnt_PDU-CD_N53-vPOD4_EO1"
            ]
         }
      ]
   }
}

To parse this JSON using bash, we can use the tool jq, which you can install by running, for example in Ubuntu:

sudo apt-get install jq

Once the tools is available, you can run the following to get the Credentials field:

CREDENTIALS=$(cat test.json | jq -r ".status.credentials")

To validate so, run:

╰─$ echo $CREDENTIALS
R3DMPF8VIAKG6xLa5vOlp7kqmqE.AAJTSQACMDIAAlNLABxXVVhKZkJhMmVOL1ZJQWhRUTBYTGY1V2w2TjA9AAR0eXBlAANDVFMAAlMx**..*
Sign up to request clarification or add additional context in comments.

1 Comment

This is great Rashad Zhran
0
CREDENTIALS=$(awk -F\" '/credentials/ {print $4}' file.json)

1 Comment

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
0

Use FS, OFS and NF to do all the hard-work, since only matching row will have NF = 3 while other non-empty rows will either be 1 or 2 :

 input : 

 {"status":
    {"reqStatus":"SUCCESS",
            "credentials":"R3DMPF8VIAKG6xLa5vOlp7kqmqE.*AAJTSQACMDIAAlNLABxXVVhKZkJhMmVOL1ZJQWhRUTBYTGY1V2w2TjA9AAR0eXBlAANDVFMAAlMx*****..*",
                    "msgs":[{"msgCode":"ECMSE103",
                            "msgText":"User %A1% was authenticated successfully.",
                            "msgValues":["Tnt_PDU-CD_N53-vPOD4_EO1"]}]
    }
}

 command :

  mawk 'NF*=2<NF' FS='^.*["]credentials["][ \t]*[:][ \t]*["]|["].*$' OFS= 

 output :

R3DMPF8VIAKG6xLa5vOlp7kqmqE.*AAJTSQACMDIAAlNLABxXVVhKZkJhMmVOL1ZJQWhRUTBYTGY1V2w2TjA9AAR0eXBlAANDVFMAAlMx*****..*

Comments

0

With jq + read and process substitution or command substitution

read var < <(jq '.status.credentials' file.json);
# or
var=$(jq '.status.credentials' file.json);
# output
echo $var
"R3DMPF8VIAKG6xLa5vOlp7kqmqE.*AAJTSQACMDIAAlNLABxXVVhKZkJhMmVOL1ZJQWhRUTBYTGY1V2w2TjA9AAR0eXBlAANDVFMAAlMx*****..*"

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.