Crystal: Nested JSON does not parse

Created on 28 Oct 2019  路  2Comments  路  Source: crystal-lang/crystal

Nested JSON objects fail to parse.

Reproducible Code

require "json"

valid_json = %({"foo": "{\"bar\":\"baz\"}"})

JSON.parse(valid_json) # => Unexpected char 'b' at 1:12 (JSON::ParseException)

Crystal 0.31.1

This also applies for mapping and serialisable.
https://carc.in/#/r/7wen
https://carc.in/#/r/7weq
https://carc.in/#/r/7weo

question

Most helpful comment

For a literal \ you need to write \\, even inside %()

https://carc.in/#/r/7wfv

All 2 comments

Not a bug.

Your example is not a nested JSON. The nested JSON is this:

valid_json = {foo: {bar: "baz" } }.to_json
p! valid_json # => "{\"foo\":{\"bar\":\"baz\"}}"
p! JSON.parse(valid_json) # => {"foo" => {"bar" => "baz"}}

If you want a string instead inside a foo key you are missing some escaping.

For a literal \ you need to write \\, even inside %()

https://carc.in/#/r/7wfv

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cjgajard picture cjgajard  路  3Comments

nabeelomer picture nabeelomer  路  3Comments

oprypin picture oprypin  路  3Comments

Papierkorb picture Papierkorb  路  3Comments

ArthurZ picture ArthurZ  路  3Comments