0

I have a JSON String in bash as follows.

sample='[{
    
    "prTitle": "Revert some thing",
    "name": "Some name"
}, {
    
    "prTitle": "Revert \"Add logs for issue analysis\"",
    "name": "Some name"
}]'

I need to process this json using jq as follows.

echo $sample | jq -c '.[]' | while read i; do
   echo $i | jq -r '.prTitle'
done

But I am getting an error like

parse error: Invalid numeric literal at line 1, column 111

I understand that this error causing due to the quoted " inside the json string. But I just want a solution to process it in a right way.

  • `echo "$sample" | jq -r '.[].prTitle'` – Inian Aug 08 '22 at 08:19
  • As `bash` is tagged, a [Here String](https://tldp.org/LDP/abs/html/x17837.html) would be another option: `jq -r '.[].prTitle' <<< "$sample"`. – pmf Aug 08 '22 at 08:27
  • @NishothanVettivel Same issue. Use quotes around **all** the bash variables. – pmf Aug 08 '22 at 08:30
  • Is the code should be as follows ```echo $sample | jq -c '.[]' | while read i; do echo "$i" | jq -r '.prTitle' done``` – Nishothan Vettivel Aug 08 '22 at 08:31
  • @NishothanVettivel See multiple linked duplicates – Inian Aug 08 '22 at 08:31
  • Double-quote *all* of the variable references. [shellcheck.net](https://www.shellcheck.net) will point them out, as well as the fact that `read` without `-r` will mangle backslashes (which is what's actually causing the error here -- the backslashes in the `prTitle` value get lost). – Gordon Davisson Aug 08 '22 at 09:26
  • It occurs to me that `echo` may also be doing weird things with backslashes (some versions do). Using here-strings (as @pmf suggested) would avoid that. Otherwise, use `printf %s\n'` instead (i.e. `printf '%s\n' "$sample" | ...`). – Gordon Davisson Aug 08 '22 at 09:44

0 Answers0