1

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 " at the end of the third line.

How can i solve this?

Tia, Anthony

gsa
  • 784
  • 1
  • 8
  • 20
Anthony
  • 13
  • 1
  • 4

4 Answers4

2

That's because the JSON format is invalid. It should look like this:

{
    "words" : "one two \nthree four five"
}
CodiMech25
  • 423
  • 3
  • 9
1

You have to escape end of line in JSON:

{
  "words" : "one two\nthree four five"
}
Michał Grzejszczak
  • 2,491
  • 1
  • 23
  • 26
  • Minor semantic nit: this is a string that includes an escape sequence that *represents* a newline character, rather than escaping a literal newline character. – chepner Aug 02 '18 at 16:14
1

To convert the text with the multi-line string to valid JSON, you could use any-json (https://www.npmjs.com/package/any-json), and pipe that into jq:

$ any-json --input-format=cson split-string.txt
{
    "words": "one two three four five"
}
$ any-json --input-format=cson split-string.txt | jq length
1

For more on handling almost-JSON texts, see the jq FAQ: https://github.com/stedolan/jq/wiki/FAQ#processing-not-quite-valid-json

peak
  • 88,177
  • 15
  • 123
  • 150
1

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

The way jq flags this error may be counterintuitive, but the error in the JSON precedes the indicated quote-mark.

If the error is non-obvious, it may be that an end-quote is missing on the prior key or value. In this case, the value that matches the criteria U+0000 through U+001F could be U+000A, which is the line feed character in ASCII.

In the case of this question, the line feed was inserted intentionally. But, unescaped, this is invalid JSON.

Brent Bradburn
  • 46,639
  • 15
  • 140
  • 156