0
{
    "PlatformID": 1024,
    "SystemId": 11640,
    "SystemName": "010.10.10.10",
    "DomainName": null,
    "AccountId": 15631,
    "AccountName": "merg1",
    "AccountNameFull": "merg1",
    "ApplicationID": null,
    "ApplicationDisplayName": null,
    "MaximumReleaseDuration": 120,
    "MaxReleaseDurationDays": 0,
    "MaxReleaseDurationHours": 2,
    "MaxReleaseDurationMinutes": 0,
    "InstanceName": "",
    "DefaultReleaseDuration": 120,
    "DefaultReleaseDurationDays": 0,
    "DefaultReleaseDurationHours": 2,
    "DefaultReleaseDurationMinutes": 0,
    "LastChangeDate": "2019-08-21T10:53:25.237",
    "NextChangeDate": null,
    "IsChanging": false,
    "IsISAAccess": false,
    "PreferredNodeID": "3ef3e7c7-5851-451b-b1a4-c62556b588ce"
}

I am looking for "SystemId and AccountId" from the above JSON response without using jq tool.

Kindly help with the shell script. Thank you.

jww
  • 97,681
  • 90
  • 411
  • 885
tata
  • 33
  • 3
  • 5
    What's preventing you from using the `jq` tool? – AVH Dec 18 '19 at 09:15
  • that jq package needs to be installed and its not installed in the linux box, jq command not found – tata Dec 18 '19 at 10:30
  • 1
    Does this answer your question? [Parsing JSON with Unix tools](https://stackoverflow.com/questions/1955505/parsing-json-with-unix-tools) – Tsyvarev Dec 19 '19 at 22:21

3 Answers3

1

You can use grep on the following usage.

grep -E -- 'AccountId|SystemId' t.txt | awk '{print $2}'
11640,
15631,

Or If would you like to use jq you can try this one.

cat t.txt | jq '.AccountId , .SystemId'
15631
11640
Adem Öztaş
  • 20,457
  • 4
  • 34
  • 42
0

If you are sure of the structure of the json file, and the following steps should work (this requires that a pair key and value is always on the same line -- if not we need to remove any white space before and after the colon)

  1. separate each field to a separate line
  2. use grep to extract the line that has SystemId or AccountId
  3. use awk to extract the second field in that line (delimited by the colon)
cat a.json | tr , '\n' | grep SystemId | awk -F ':' '{print $2}'
11640

cat a.json | tr , '\n' | grep AccountId | awk -F ':' '{print $2}'
15631

Edited: if json is in a varaible, and there is a need to save the output to variable:

#!/bin/bash

JSON=$(cat a.json)


sysID=$(echo $JSON | tr , '\n' | grep SystemId | awk -F ':' '{print $2}')
accID=$(echo $JSON | tr , '\n' | grep AccountId | awk -F ':' '{print $2}')

echo "System ID = $sysID, Account ID = $accID"

# output
# System ID =  11640, Account ID =  15631

9mat
  • 1,194
  • 9
  • 13
  • How to I do the same thing if the json is in the variable not in the file? and once parse I need to assign it to variable like sysID and accID instead of print – tata Dec 18 '19 at 10:28
  • if the json is in the variable, use `echo $var` instead of `cat a.json`. To assign it to a variable, use `$( )`. I will update the answer to give more details. – 9mat Dec 18 '19 at 10:57
  • Super its working, this is what i was looking for... thank you so much...! – tata Dec 18 '19 at 11:20
0
sysID=$(echo $JSON | tr , '\n' | grep SystemId | awk -F ':' '{print $2}')
accID=$(echo $JSON | tr , '\n' | grep AccountId | awk -F ':' '{print $2}')

echo "System ID = $sysID, Account ID = $accID"

This worked for me...! without using jq tool

jww
  • 97,681
  • 90
  • 411
  • 885
tata
  • 33
  • 3