Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why this json string cannot be parsed? #365

Closed
NeroBlackstone opened this issue Apr 22, 2024 · 1 comment
Closed

Why this json string cannot be parsed? #365

NeroBlackstone opened this issue Apr 22, 2024 · 1 comment

Comments

@NeroBlackstone
Copy link

{
	"contents": "While \u003Ca href=\"https://www.pcgamer.com/uk/search/?searchTerm=team+fortress+2\" "
}
using JSON

json = """
{
	"contents": "While \u003Ca href=\"https://www.pcgamer.com/uk/search/?searchTerm=team+fortress+2\" "
}
"""

JSON.parse(json)

Then throw a error:

ERROR: Expected ',' here
Line: 1
Around: ...<a href="https://www.pcgamer.c...
                    ^

Stacktrace:
 [1] _error(message::String, ps::JSON.Parser.MemoryParserState)
   @ JSON.Parser ~/.julia/packages/JSON/93Ea8/src/Parser.jl:140
 [2] _error_expected_char(c::UInt8, ps::JSON.Parser.MemoryParserState)
   @ JSON.Parser ~/.julia/packages/JSON/93Ea8/src/Parser.jl:83
 [3] skip!
   @ ~/.julia/packages/JSON/93Ea8/src/Parser.jl:80 [inlined]
 [4] parse_object(pc::JSON.Parser.ParserContext{Dict{String, Any}, Int64, true, nothing}, ps::JSON.Parser.MemoryParserState)
   @ JSON.Parser ~/.julia/packages/JSON/93Ea8/src/Parser.jl:234
 [5] parse_value(pc::JSON.Parser.ParserContext{Dict{String, Any}, Int64, true, nothing}, ps::JSON.Parser.MemoryParserState)
   @ JSON.Parser ~/.julia/packages/JSON/93Ea8/src/Parser.jl:166
 [6] parse(str::String; dicttype::Type, inttype::Type{Int64}, allownan::Bool, null::Nothing)
   @ JSON.Parser ~/.julia/packages/JSON/93Ea8/src/Parser.jl:450
 [7] parse(str::String)
   @ JSON.Parser ~/.julia/packages/JSON/93Ea8/src/Parser.jl:443
 [8] top-level scope
   @ REPL[4]:1

Why? I think this is a validated json string.

@yuyichao
Copy link
Contributor

This is just julia string parsing. The \" in your string literal resulted in just a ".

julia> using JSON

julia> json = """
       {
               "contents": "While \u003Ca href=\"https://www.pcgamer.com/uk/search/?searchTerm=team+fortress+2\" "
       }
       """;

julia> json2 = """
       {
               "contents": "While \u003Ca href=\\"https://www.pcgamer.com/uk/search/?searchTerm=team+fortress+2\\" "
       }
       """;

julia> println(json)
{
        "contents": "While <a href="https://www.pcgamer.com/uk/search/?searchTerm=team+fortress+2" "
}


julia> println(json2)
{
        "contents": "While <a href=\"https://www.pcgamer.com/uk/search/?searchTerm=team+fortress+2\" "
}


julia> JSON.parse(json)
ERROR: Expected ',' here
Line: 1
Around: ...<a href="https://www.pcgamer.c...
                    ^

Stacktrace:
<......>

julia> JSON.parse(json2)
Dict{String, Any} with 1 entry:
  "contents" => "While <a href=\"https://www.pcgamer.com/uk/search/?searchTerm=tea…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants