Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are there invalid characters in this JSON?

My JSON looks something like this:

{
  "Master" : {
    "Major" : "S",
    "Minor" : "E",
    "IPAddress" : "0.0.0.0",
    "Detail":"<root>
               <key keyname=\"state\">3</key>   
               <key keyname=\"oldState\">1</key>        
               <key keyname=\"currency\"></key>
               <key keyname=\"denomination\"></key></root>",    
    "SourceCreateDate" : "2014-04-03T14:02:57.182+0200"
   },
   "Messages" : [{
    "MessageCode" : "0",                    
    "MessageType" : "8"
   }]
}

I'm getting an 'Invalid Characters found' error when validating this. Where are the invalid characters and how can I make this JSON valid?

like image 963
user906153 Avatar asked Dec 02 '22 15:12

user906153


2 Answers

{
"Master": {
    "Major": "S",
    "Minor": "E",
    "IPAddress": "0.0.0.0",
    "Detail": "<root><key keyname=\"state\">3</key><key keyname=\"oldState">1</key><key keyname=\"currency\"></key><key keyname=\"denomination\"></key></root>",
    "SourceCreateDate": "2014-04-03T14:02:57.182+0200"
},
"Messages": [
    {
        "MessageCode": "0",
        "MessageType": "8"
    }
]
}

JSON validator: http://jsonlint.com/

Edit: Explication: when you open a " you need to close it on the same line. So you have to put your xml on a single line or to escape it.

like image 188
Marchah Avatar answered Dec 04 '22 05:12

Marchah


JSON only accepts single line Strings.

A work-around would be:

"Detail": [
    "<root>",
    ",<key keyname=\"state\">3</key>", 
    "<key keyname=\"oldState\">1</key>",        
    "<key keyname=\"currency\"></key>",
    "<key keyname=\"denomination\"></key></root>"
], 

You also have the option to replace line breaks into \n.

like image 31
morkro Avatar answered Dec 04 '22 04:12

morkro