JuliaIO / JSON.jl

JSON parsing and printing
Other
311 stars 100 forks source link

failure to parse key/value strings containing $ character #318

Closed combinadic closed 3 years ago

combinadic commented 3 years ago

Greetings. When trying to parse that contains the $ character JSON.parse() produces the following output:

JSON.parse("""{ "this": ["is", "json"], "numbers": [85, 16, 12.0], "and": [true, false, null], "but not": ["1", 2,"$3.00"]}""") ERROR: syntax: invalid interpolation syntax: "$3" Stacktrace: [1] top-level scope at none:1

However, removing the "$" from the last key/value pair produces the expected result. Have I overlooked something in the JSON.parse() syntax? Short of replace(ing)() the "$" with something like "AUD" character in pre-processing, is there some other workaround? Thank you.

KristofferC commented 3 years ago

Writing a $ in a string literal is Julia syntax for interpolating something into the string (see https://docs.julialang.org/en/v1/manual/strings/#string-interpolation). You can see that you get the error, even if you remove the JSON.parse part. The solution is to either escape the $ (via \$) or use a raw string (via raw"""... $3.00...""").