Response element has HTML Content - final JSON response is getting appended with additional tags

Not applicable

We have a system that provides a response as a SOAP message. The API Proxy in Apigee Edge converts the SOAP/Xml to JSON and sends the JSON response to consumer.

In the Response, the "contentText" element in the XML has HTML content. The final JSON response sent to consumer is getting appended with "\n" (new line) & "\t" (tab) in the HTML content as highlighted below.

We do not want to touch the HTML content in the "contentText" and send the original content as is to consumer.

Really appreciate any advise or suggestions. Thank you!

{
  "response": {
    "status": {
      "responseCode": "00000"
    },
    "responseDetails": {
      "identifier": "1234567",
      "contentText": "<POLICY><TITLE><![CDATA[Ploicies]]></TITLE><OVERVIEW><![CDATA[<p>The policy is in place to encourage</p>\n\n<ul>\n\t<li>Preferred</li>\n\t</ul>\n]]></OVERVIEW><TITLE></POLICY>"
    }
  }
}

1 2 4,045
2 REPLIES 2

Yes, I understand what you're saying.

The problem you are experiencing is due to how JSON encodes text. What you are seeing is not an error, it's correct behavior. Perhaps your expectations are not valid.

In JSON newlines and tabs must be encoded in strings as \n and \t. You can read about this on the json site.

The conclusion: if you encode a value that has newlines and tabs, then ... that's what you will get. The receiving app must be smart enough to handle the embedded newlines and tabs. Either decode them, or replace them with the appropriate thing for the app scenario.

Just one more comment though:

I think there is a problem with the handling of the CDATA in the XML payload. In my opinion the CDATA should not be passed through into JSON. Rather, the JSON string should be free of CDATA fields. CDATA markup is an XML thing, and it doesn't belong in JSON at all.

In other words, the payload I would expect, would be something like this:

{
  "response": {
    "status": {
      "responseCode": "00000"
    },
    "responseDetails": {
      "identifier": "1234567",
      "contentText": "<POLICY><TITLE>Ploicies></TITLE><OVERVIEW><p>The policy is in place to encourage</p>\n\n<ul>\n\t<li>Preferred</li>\n\t</ul>\n</OVERVIEW><TITLE></POLICY>"
    }
  }
}

I don't know what the input XML looks like, I'm assuming.

Let me test this, and if the behavior is as I suspect, I will raise a bug.

But still you will need to deal with the encoded newlines and tabs in the JSON, on the consumer app.

EDIT #2.

ok I just tested this and the behavior I had suspected was occurring, based on your report, is not actually occurring for me. When I send an XML doc with a CDATA section into an XMLToJSON policy, I get JSON that does not include the CDATA.

$ curl -i https://org-test.apigee.net/echo-xmltojson -H content-type:text/xml -d '<root><![CDATA[foo]]></root>'
HTTP/1.1 200 OK
Date: Thu, 04 May 2017 19:20:42 GMT
Content-Type: application/json;charset=UTF-8
Content-Length: 20
Server: Apigee Router


{
  "root": "foo"
}

This is different than the payload you are reporting. I don't know why that would be. If you want to investigate that, please open a new question and post your source XML.

Hi @Dino,

I have a scenario, Backend is expecting the SOAP with the CDATA in one of the field and Front end is sending the JSON to the proxy. i have two queries.

1.Does we need to ask the frontend to send the CDATA(in my case value in cdata is XML) in the JSON as a normal string ?

2.How do i convert JSON to XMl with the CDATA field contained in it ?

Need your comments here 🙂