0

I'm trying to parse the below json array and get the values from it. But it is not working with spaces as values. I did find some solutions in stackoverflow but nothing seems to work.

JSON

 {
  "apps": [
    {
      "name": "Root Certification Authority - G2",
      "expiryDate": "Monday, 09 October 2023 20:03:25",
      "impactStatement": "Apps using this root certificate will have an actual impact",
      "notifyBeforeInDays": 60
    },
    {
      "name": "Bamboo",
      "expiryDate": "Sunday, 20 November 2022 03:25:23",
      "impactStatement": "CI/CD wont be working",
      "sop": "https://somelink/Bamboo+SOPs",
      "notifyBeforeInDays": 30
    },
    {
      "name": "Vault - Client",
      "expiryDate": "Monday, 09 October 2023 20:03:25",
      "impactStatement": "All Mule applications for that particular environment will stop working",
      "notifyBeforeInDays": 60
    },
    {
      "name": "Consul",
      "expiryDate": "Monday, 21 August 2023 14:43:56",
      "impactStatement": "No Direct impact or never had any such scenario so far",
      "notifyBeforeInDays": 30
    },
    {
      "name": "bitbucket",
      "expiryDate": "08 September 2021 13:16:06",
      "impactStatement": "No Impact",
      "notifyBeforeInDays": 15
    }
  ]
}

And I'm using the below code to parse the json

appls=$(awk '{print $0}' ${work_dir}/scripts/applications.json | jq -c '. |select(.apps !=null) |.apps[]')
echo "*****"
echo $appls
echo "*****"
for row in ${appls}; do
  echo $row
  validateAndNotify $row
done

And when i print the above variable following output is printed, which is not a valid one.

*****
 {"name":"bitbucket","expiryDate":"08 September 2021 13:16:06","impactStatement":"No Impact","notifyBeforeInDays":15} such scenario so far","notifyBeforeInDays":30}","notifyBeforeInDays":60}Bamboo+SOPs","notifyBeforeInDays":30}
*****
{"name":"Root

I want to parse the apps array and get each value inside that node.

1
  • 1
    btw that split is fully baroque, just jq '.apps[]?' that/file Commented Nov 26, 2020 at 0:17

1 Answer 1

2

You need to quote your variables in the shell to prevent from word splitting:

#!/bin/bash

work_dir="/foo/bar"

validateAndNotify() {
    echo "${1}"
}

appls=$(jq -c '.apps[]?' "${work_dir}/scripts/applications.json")

echo "*****"
echo "${appls}"
echo "*****"
while read -r row ; do
  validateAndNotify "${row}"
done <<< "${appls}"

PS: Shortened the jq command. Thanks jthill

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

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.