4

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?

4 Answers 4

7
{
"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.

1
  • This shows the/a correct formation for the OP's JSON to work, but doesn't explain anything — why does this work and the OP's doesn't work? Reading the other answers makes it clear why this is correct, but this answer should stand on its own.
    – Stephen P
    Commented Jul 31, 2014 at 17:15
6

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.

5

The invalid characters are the line breaks in the "Detail" element. You'll need to escape them. Something like the solution presented here should work.

0

try 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 think there were some hidden junk characters were there, like next line(line breaks) or tab spaces etc.. that why it was giving error. so make sure "Details" key will be there in single line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.