0

I am calling nodejs function from bash script which returns JSON output like below:

[ { fullyQualifiedDomainName: 'XXXXXXXX',
   hostname: 'XXXXXXXX',
   id: XXXXXXXXxxxx,
   primaryBackendIpAddress: 'XXXXXXXXxx',
   primaryIpAddress: 'XXXXXXXXxx' },
 { fullyQualifiedDomainName: 'XXXXXXXX',
   hostname: 'XXXXXXXX',
   id: XXXXXXXXxxxx,
   primaryBackendIpAddress: 'XXXXXXXXxx',
   primaryIpAddress: 'XXXXXXXXxx' },
{ fullyQualifiedDomainName: 'XXXXXXXX',
   hostname: 'XXXXXXXX',
   id: XXXXXXXXxxxx,
   primaryBackendIpAddress: 'XXXXXXXXxx',
   primaryIpAddress: 'XXXXXXXXxx' },
{ fullyQualifiedDomainName: 'XXXXXXXX',
   hostname: 'XXXXXXXX',
   id: XXXXXXXXxxxx,
   primaryBackendIpAddress: 'XXXXXXXXxx',
   primaryIpAddress: 'XXXXXXXXxx' }]

I want to retrieve all the ids from above output. Is there a way to do this? Output can have details for n number of devices.

ceving
  • 21,900
  • 13
  • 104
  • 178

2 Answers2

1

In your bash script , at loop take those id to an array then give that command to run your Nodejs script with parameter

node index.js -idArray [your id data like 1,2,3,4]

Put this code to your Nodejs file to use your array

let argv = require('minimist')(process.argv); console.dir(argv.idArray);

erdemgunenc
  • 969
  • 3
  • 14
  • 25
  • how to get ids from this output in bash script? – user3655005 Aug 07 '18 at 11:03
  • 1
    If it's type is string you can string match in loop then push to an array.Search some bash script string match if type is string or look some loop operations.I guess you can accept above code as answer – erdemgunenc Aug 07 '18 at 11:34
0

Your example is not JSON. This would be JSON:

[{ "fullyQualifiedDomainName": "XXXXXXXX",
   "hostname": "XXXXXXXX",
   "id": "XXXXXXXXxxxx1",
   "primaryBackendIpAddress": "XXXXXXXXxx",
   "primaryIpAddress": "XXXXXXXXxx" },
 { "fullyQualifiedDomainName": "XXXXXXXX",
   "hostname": "XXXXXXXX",
   "id": "XXXXXXXXxxxx2",
   "primaryBackendIpAddress": "XXXXXXXXxx",
   "primaryIpAddress": "XXXXXXXXxx" },
 { "fullyQualifiedDomainName": "XXXXXXXX",
   "hostname": "XXXXXXXX",
   "id": "XXXXXXXXxxxx3",
   "primaryBackendIpAddress": "XXXXXXXXxx",
   "primaryIpAddress": "XXXXXXXXxx" },
 { "fullyQualifiedDomainName": "XXXXXXXX",
   "hostname": "XXXXXXXX",
   "id": "XXXXXXXXxxxx4",
   "primaryBackendIpAddress": "XXXXXXXXxx",
   "primaryIpAddress": "XXXXXXXXxx" }]

And in this case jq is the first choice to query values form JSON data.

$ jq '.[].id' json
"XXXXXXXXxxxx1"
"XXXXXXXXxxxx2"
"XXXXXXXXxxxx3"
"XXXXXXXXxxxx4"

Or use the raw output to store the result in an Bash array:

$ ids=($(jq -r '.[].id' json))
$ printf "%s\n" "${ids[@]}"
XXXXXXXXxxxx1
XXXXXXXXxxxx2
XXXXXXXXxxxx3
XXXXXXXXxxxx4
ceving
  • 21,900
  • 13
  • 104
  • 178