0

I want to transform JSON file into bash array of strings that i will later be able to iterate over. My JSON structure is as follows:

[
  {
    "USERID": "TMCCP",
    "CREATED_DATE": "31/01/2020 17:52"
  },
  {
    "USERID": "TMCCP",
    "CREATED_DATE": "31/01/2020 17:52"
  }
]

And this is my bash script:

test_cases=($(jq -c '.[]' data.json))
echo ${test_cases[0]}
echo ${test_cases[1]}
echo ${test_cases[2]}
echo ${test_cases[3]}

As you can see it returns array with 4 elements instead of 2. Output:

{"USERID":"TMCCP","CREATED_DATE":"31/01/2020
17:52"}
{"USERID":"TMCCP","CREATED_DATE":"31/01/2020
17:52"}

For some reason having whitespace in date field causes some parsing issues. Any idea how to get over this?

2 Answers 2

2

Use readarray instead.

$ readarray -t test_cases < <(jq -c '.[]' file)
$ declare -p test_cases
declare -a test_cases=([0]="{\"USERID\":\"TMCCP\",\"CREATED_DATE\":\"31/01/2020 17:52\"}" [1]="{\"USERID\":\"TMCCP\",\"CREATED_DATE\":\"31/01/2020 17:52\"}")

And read can be used as shown below where readarray is unavailable.

IFS=$'\n' read -d '' -a test_cases < <(jq -c '.[]' file)
Sign up to request clarification or add additional context in comments.

Comments

1

Use readarray to populate the array, rather than using an unquoted command substitution; bash doesn't care about JSON quoting when it splits the result into separate words.

readarray -t test_cases < <(jq -c '.[]' data.json)

In bash 3.2 (which is what you appear to be stuck with), you need something slightly more unwieldy

while IFS= read -r line; do
    test_cases+=("$line")
done < <(jq -c '.[]' data.json)

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.