Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse error when text is split on multi lines: "control characters from U+0000 through U+001F must be escaped"

Tags:

json

jq

I'm getting a "parse error" when I split a text line on multiple lines and show the JSON file on screen with the command jq . words.json.

The JSON file with the text value on a single line looks like this

{
    "words" : "one two three four five"
}

The command jq . words.json works fine and shows the JSON file on screen.

But when I split the value "one two three four five" on two lines and run the same command I get a parse error

{
    "words" : "one two   
    three four five"
                   ^
}  
                 

parse error: Invalid string: control characters from U+0000 through U+001F must be escaped at line 3, column 20

The parse error points to the " character at the end of the third line.

How can I solve this?

like image 478
Anthony Avatar asked Dec 03 '25 16:12

Anthony


1 Answers

That's not valid JSON; JSON strings don't contain literal newlines. But you can do:

jq -Rnr '[inputs] | join("\\n") | fromjson | .choices[0].message.content' sample.json
like image 90
oguz ismail Avatar answered Dec 06 '25 06:12

oguz ismail



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!