0

When making query using Jq Play for the provided json the output looks as expected Demo. But when I try the same query in shell Script & iterate the object I see new row got added because of whitespace between sentence.

Query:

query=$(cat $basename/test.json | jq -r '.DesignCode | to_entries[] | "\(.key):\(.value)"')

for i in $query

do
    printf "$i"
done

used in the shell script

Output ScreenShot

enter image description here

What is the correct way to write the query?

Michael M.
  • 10,486
  • 9
  • 18
  • 34
Anand
  • 2,086
  • 2
  • 26
  • 44
  • 2
    Does this answer your question? [Looping through the content of a file in Bash](https://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash) – Aserre Jul 11 '22 at 08:35
  • You can use the same logic for a function and for a file : `while IFS= read -r line; do echo "$line"; done < <(jq -r '....' input.json)` – Aserre Jul 11 '22 at 08:36

1 Answers1

0

I'm not sure about the output of your command but, in my experience, shell is a bit confusing when it comes to creating arrays from strings.

A useful workaround I use a lot is forcing shell to recognize the output as an array by compound assignment:

query=( $(cat $basename/test.json | jq -r '.DesignCode | to_entries[] | "\(.key):\(.value)"') )

for i in $query

do
    printf "$i \n"
done
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83