storing xml inside json object
Asked Answered
M

3

27

I need to store complete XML document as part of JSON object. when i receive the request and try to create JSON object from JSON string like below:

{"content":{
"name" : "xyz",
"details":"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
 <ns0:Report xmlns:ns0=\"http://www.khisko.com/triTypes\">
  <StackTrace>Job-8004 Error in [xxxxxxxxxx]
      Output data invalid&#xD;
  at com.xyz.tst.a(Unknown Source)&#xD;
      caused by: java.lang.NullPointerException&#xD;
   </StackTrace>
   <Msg>Output data invalid</Msg>
  </ns0:Report>"
 }}

I am getting Unterminated string error at first char of details. How can I handle it?

I am using org.json.JSONObject constructor which takes java string as parameter and passing above JSON as java string.

Meza answered 3/6, 2013 at 21:19 Comment(4)
Remove the line breaks from the xmlEdacity
and why in gods name do you want to put xml inside json ?Humes
xml is request received from client which i need to preserve.Meza
@Humes - because you need to support a new feature in an existing code base without disrupting other existing functionality :( ;)Potion
B
31

You can encode and decode xml string like

{
  "content": {
    "name": "xyz",
    "details": "PD94bWwgdmVyc2lvbj1cIjEuMFwiIGVuY29kaW5nPVwiVVRGLThcIj8+CiA8bnMwOlJlcG9ydCB4bWxuczpuczA9XCJodHRwOi8vd3d3LmtoaXNrby5jb20vdHJpVHlwZXNcIj4KICA8U3RhY2tUcmFjZT5Kb2ItODAwNCBFcnJvciBpbiBbeHh4eHh4eHh4eF0KICAgICAgT3V0cHV0IGRhdGEgaW52YWxpZCYjeEQ7CiAgYXQgY29tLnh5ei50c3QuYShVbmtub3duIFNvdXJjZSkmI3hEOwogICAgICBjYXVzZWQgYnk6IGphdmEubGFuZy5OdWxsUG9pbnRlckV4Y2VwdGlvbiYjeEQ7CiAgIDwvU3RhY2tUcmFjZT4KICAgPE1zZz5PdXRwdXQgZGF0YSBpbnZhbGlkPC9Nc2c+CiAgPC9uczA6UmVwb3J0Pg==",
    "encoding": "base64"
  }
}
Bacitracin answered 21/1, 2019 at 10:27 Comment(4)
To do encoding and decoding in client JavaScript use atob() and btoa(). Here is MDN doc link: developer.mozilla.org/en-US/docs/Web/API/WindowBase64/…Hygrometer
I was wondering why this answer was not marked as the right answer until I saw it is posted 6 years after the question :)Poulin
I don't find this answer clear tbh. You have hashed the XML? What if you get that xml from a db and you have to store it as it is in the json file?Spangle
@Spangle looks like base64 encoding to me. Just google base64 to find the appropriate .Net call. e.g. learn.microsoft.com/en-us/dotnet/api/… e.g. code: public static String ToBase64String(this String source) { return Convert.ToBase64String(Encoding.Unicode.GetBytes(source)); }Potion
O
14

Just I've changed \" to ' and remove line breaks like @Explosion Pills says

{"content":{
    "name" : "xyz",
    "details":"<?xml version='1.0' encoding='UTF-8'?>
     <ns0:Report xmlns:ns0='http://www.khisko.com/triTypes'>
      <StackTrace>Job-8004 Error in [xxxxxxxxxx]
          Output data invalid&#xD;
      at com.xyz.tst.a(Unknown Source)&#xD;
          caused by: java.lang.NullPointerException&#xD;
       </StackTrace>
       <Msg>Output data invalid</Msg>
      </ns0:Report>"
     }}
Orthoclase answered 21/12, 2015 at 21:24 Comment(1)
actually the original question is a duplicate of #9264970 but that Thread has the answer hidden in a comment. I have a similar need and will try this approach, I guess this comes closest to XML's CDATA element.Brittneybrittni
M
6

I didn't want to remove the line breaks from the XML so I've changed the Java class that handles the JSON to not throw unterminated string exceptions for newlines or carriage returns.

Meza answered 6/6, 2013 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.