2

I got a json string as a result of ajax request. And I need to convert json into xml string using javascript/jquery. Please help me. Json string like below:

var json =
    "{
      "Data": {
        "SOM": {
          "Tab": [
            {
              "Values": {
                "SelectedValues": null,
                "LoadedValues": null,
                "ExpandedValues": null,
                "ID": "msorgrole"
              },
              "ID": "OrgRole"
            },
            {
              "Values": {
                "SelectedValues": null,
                "LoadedValues": null,
                "ExpandedValues": null,
                "ID": "msorg"
              },
              "ID": "Organization"
            },
            {
              "Values": {
                "SelectedValues": null,
                "LoadedValues": null,
                "ExpandedValues": null,
                "ID": "mscontenttype"
              },
              "ID": "PeopleType"
            },
            {
              "Values": {
                "SelectedValues": ",B79720D5-0E95-4CB7-B4F9-37BE24696F4F,831A2A77-B758-493A-B0F4-991A6427C31C,",
                "LoadedValues": null,
                "ExpandedValues": null,
                "ID": "mspeople"
              },
              "ID": "People"
            }
          ]
        }
      }
    }"

I need to convert the above json string like below:

var json = "<Data><SOM><Tab ID="OrgRole"> <Values ID="msorgrole"><SelectedValues /> <LoadedValues /> <ExpandedValues /></Values></Tab><Tab ID="Organization"> <Values ID="msorg"><SelectedValues /> <LoadedValues /> <ExpandedValues /></Values></Tab><Tab ID="PeopleType"> <Values ID="mscontenttype"><SelectedValues /> <LoadedValues /> <ExpandedValues /></Values></Tab><Tab ID="People"> <Values ID="mspeople"><SelectedValues>,831A2A77-B758-493A-B0F4-991A6427C31C,B79720D5-0E95-4CB7-B4F9-37BE24696F4F,</SelectedValues> <LoadedValues /> <ExpandedValues /></Values></Tab></SOM></Data>"
2

4 Answers 4

2

You can use http://goessner.net/download/prj/jsonxml/ like this using the function json2xml:

var data = '{"Data":{"SOM":{"Tab":[{"Values":{"SelectedValues":null,"LoadedValues":null,"ExpandedValues":null,"ID":"msorgrole"},"ID":"OrgRole"},{"Values":{"SelectedValues":null,"LoadedValues":null,"ExpandedValues":null,"ID":"msorg"},"ID":"Organization"},{"Values":{"SelectedValues":null,"LoadedValues":null,"ExpandedValues":null,"ID":"mscontenttype"},"ID":"PeopleType"},{"Values":{"SelectedValues":",B79720D5-0E95-4CB7-B4F9-37BE24696F4F,831A2A77-B758-493A-B0F4-991A6427C31C,","LoadedValues":null,"ExpandedValues":null,"ID":"mspeople"},"ID":"People"}]}}}';

var jsonObj = JSON.parse(data); // important to first convert json string into object

alert(json2xml(jsonObj));
<script src="http://goessner.net/download/prj/jsonxml/json2xml.js"></script>

Sign up to request clarification or add additional context in comments.

2 Comments

Hi @prtdomingo its working fine but instead of adding script tag i just copied the function from the link and used it. Thanks.
No worries, happy coding!
0

You can use this plugin the it is very effective : goessner

Comments

0

This will help you : jasontoxml

2 Comments

then can you tell me the exact issue that u r facing ?
@KishanOza any object inside an array evaluates in such a way that the node name is index of that object inside array. Example {myArray:[{Hi,Hello}]} results into <0>Hii</0> <1>Hello</1>which is not expected behavior. It should evolve to something like <myArray>Hii</myArray> <myArray>Hello</myArray>
0

Check out this https://github.com/javadev/xml-to-json

I am the maintainer of the library.

var xml = jsonToXml("{}");

// <?xml version="1.0" encoding="UTF-8"?>
// <root></root>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.