24

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.

Thanks

chappalprasad
  • 725
  • 3
  • 14
  • 23

3 Answers3

26

You can encode and decode xml string like

{
  "content": {
    "name": "xyz",
    "details": "PD94bWwgdmVyc2lvbj1cIjEuMFwiIGVuY29kaW5nPVwiVVRGLThcIj8+CiA8bnMwOlJlcG9ydCB4bWxuczpuczA9XCJodHRwOi8vd3d3LmtoaXNrby5jb20vdHJpVHlwZXNcIj4KICA8U3RhY2tUcmFjZT5Kb2ItODAwNCBFcnJvciBpbiBbeHh4eHh4eHh4eF0KICAgICAgT3V0cHV0IGRhdGEgaW52YWxpZCYjeEQ7CiAgYXQgY29tLnh5ei50c3QuYShVbmtub3duIFNvdXJjZSkmI3hEOwogICAgICBjYXVzZWQgYnk6IGphdmEubGFuZy5OdWxsUG9pbnRlckV4Y2VwdGlvbiYjeEQ7CiAgIDwvU3RhY2tUcmFjZT4KICAgPE1zZz5PdXRwdXQgZGF0YSBpbnZhbGlkPC9Nc2c+CiAgPC9uczA6UmVwb3J0Pg==",
    "encoding": "base64"
  }
}
bitsabhi
  • 698
  • 8
  • 12
  • 5
    To do encoding and decoding in client JavaScript use atob() and btoa(). Here is MDN doc link: https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding – Kirill Malakhov Aug 11 '19 at 18:15
  • 2
    I was wondering why this answer was not marked as the right answer until I saw it is posted 6 years after the question :) – Conffusion May 13 '20 at 14:12
  • 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? – Zizzipupp Jan 14 '21 at 11:27
  • @Zizzipupp looks like base64 encoding to me. Just google base64 to find the appropriate .Net call. e.g. https://docs.microsoft.com/en-us/dotnet/api/system.convert.tobase64string?view=net-5.0 e.g. code: public static String ToBase64String(this String source) { return Convert.ToBase64String(Encoding.Unicode.GetBytes(source)); } – Zeek2 Apr 06 '21 at 10:00
13

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>"
     }}
AITAALI_ABDERRAHMANE
  • 2,439
  • 1
  • 24
  • 30
  • actually the original question is a duplicate of http://stackoverflow.com/questions/9264470/is-it-possible-to-write-xml-into-json-object 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. – Gregor May 05 '17 at 10:32
4

Don't want to remove the line breaks from the xml but made change in json java class to not throw unterminated string exception for NL, CR. thanks Explosion Pills.

chappalprasad
  • 725
  • 3
  • 14
  • 23