2

I am using json library to convert json to xml but while converting I want to ignore a nested json object to be converted to xml tags.

eg.

Plane json is as :

{"id":"9568","name":"Customer Analysis","group":"demo","param":{"globalSettings":{"showLegends":false,"legendPosition":"bottom center"}}}

JSONObject json = new JSONObject("{\"id\":\"9568\",\"name\":\"Customer Analysis\",\"group\":\"demo\",\"param\":{\"globalSettings\":{\"showLegends\":false,\"legendPosition\":\"bottom center\"}}}");

    String xml = XML.toString(json);
    System.out.println(xml);

Now in above example, I want in xml with a json as it is inside. Whereas now various elements are created for showLegends and legendPosition inside globalSettings.

Current XML is as follows :

<name>Customer Analysis</name>
<id>9568</id>
<group>demo</group>
<param>
  <globalSettings>
     <showLegends>false</showLegends>
     <legendPosition>bottom center</legendPosition>
  </globalSettings>
</param>

Expected XML should be as follows :

<name>Customer Analysis</name>
<id>9568</id>
<group>demo</group>
<param>
  <globalSettings>
     {"showLegends":false,"legendPosition":"bottom center"}
  </globalSettings>
</param>

How can I handle this?

Pratik Rawlekar
  • 327
  • 4
  • 14

4 Answers4

2

I think you need to tweak JSON before converting. Can you try this below?

String json = "{\n" +
        "   \"user\": \"gerry\",\n" +
        "   \"likes\": [1, 2, 4],\n" +
        "   \"followers\": {\n" +
        "      \"options\": {\n" +
        "        \"key1\": \"a\",\n" +
        "        \"key2\": \"b\"\n" +
        "      }        \n" +
        "   }\n" +
        "}";

JSONObject jsonObject = new JSONObject(json);
JSONObject followers = jsonObject.getJSONObject("followers");

String options = followers.optString("options");
followers.put("options", options);

String s = XML.toString(jsonObject);
System.out.println(XML.unescape(s));

result:

<followers><options>{"key1":"a","key2":"b"}</options></followers><user>gerry</user><likes>[1,2,4]</likes>


Extra Question:

What if I don't want options as an xml element and it should be part of json?

String json = "{\n" +
        "   \"user\": \"gerry\",\n" +
        "   \"likes\": [1, 2, 4],\n" +
        "   \"followers\": {\n" +
        "      \"options\": {\n" +
        "        \"key1\": \"a\",\n" +
        "        \"key2\": \"b\"\n" +
        "      }        \n" +
        "   }\n" +
        "}";

JSONObject jsonObject = new JSONObject(json);
jsonObject.put("followers", jsonObject.optString("followers"));

// org.json 20180813
String s = XML.toString(jsonObject);
System.out.println(XML.unescape(s));

result:

<followers>{"options":{"key1":"a","key2":"b"}}</followers><user>gerry</user><likes>1</likes><likes>2</likes><likes>4</likes>
Madplay
  • 1,027
  • 1
  • 13
  • 25
  • Thanks for your response. I am not able to find unescape() on XML but we can achieve it with StringEscapeUtils.unescapeXml(s) – Pratik Rawlekar Apr 30 '19 at 13:50
  • When I try to convert this xml to json using JSONObject obj = XML.toJSONObject(s) , \ will be printed in the options string. How can I overcome this? Also in above solution given by you, What if I don't want options as an xml element and it should be part of json? – Pratik Rawlekar May 02 '19 at 06:49
  • @PratikRawlekar I tested it, but \ was not printed. I used org.json version ```20180813```. – Madplay May 02 '19 at 23:55
  • My current version is 20150729 but I also tried with udpdated version 20180813. Still facing the same issue when I convert it to json from xml. – Pratik Rawlekar May 03 '19 at 07:03
0

You need to modify your json a bit:

    String json = "{\"user\":\"gerry\",\"likes\":[1,2,4],\"followers\":{\"options\":\"{key1:a,key2:b}\"}}";

    JSONObject jsonObject = new JSONObject(json);
    String xml = XML.toString(jsonObject);

    System.out.println(XML.unescape(xml));

note that the "options" should be as "String" (shaped like JSON), then the XML parser will treat it as a regular string.

Adi Ohana
  • 927
  • 2
  • 13
  • 18
0

You either need to tweak the JSON before conversion, or tweak the XML after conversion.

Since such tweaking is so often required, one approach is to do the conversion using XSLT 3.0 so you have transformation capability built-in to the tool.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
0

Underscore-java library has static methods U.fromJson(json) and U.toXml(map). You may modify map and generate xml. I am the maintainer of the project.

    Map<String, Object> map = U.fromJsonMap("{\"id\":\"9568\",\"name\":\"Customer Analysis\",\"group\":\"demo\",\"param\":{\"globalSettings\":{\"showLegends\":false,\"legendPosition\":\"bottom center\"}}}");
    U.set(map, "param.globalSettings", U.toJson((Map<String, Object>) U.get(map, "param.globalSettings")));
    System.out.println(U.toXml(map));

Output:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <id>9568</id>
  <name>Customer Analysis</name>
  <group>demo</group>
  <param>
    <globalSettings>{
  "showLegends": false,
  "legendPosition": "bottom center"
}</globalSettings>
  </param>
</root>
Valentyn Kolesnikov
  • 2,029
  • 1
  • 24
  • 31