Home > Software design >  JSON.parse(message) fails but "https://jsonlint.com/" shows that the message is valid. Why
JSON.parse(message) fails but "https://jsonlint.com/" shows that the message is valid. Why

Time:11-13

It fails here,

message = "{\"response\":{\"billDetails\":\"[{\"name\":\"Account ID\"}]\"}}"
JSON.parse(message)
JSON::ParserError (809: unexpected token at '{"response":{"billDetails":"[{"name":"Account ID"}]"}}')

CodePudding user response:

I'm not sure this will be helpful. But your json string should be written from

"{
  "response":{
    "billDetails": "[{"name":"Account ID"}]"}
}"

To:

'{
  "response":{
    "billDetails": [{"name":"Account ID"}]
    }
}'

In the array from billDetails, you have two aditional ", that provokes an error when parsing that string.

  • Related