Issues converting a string containing xml to json

Hello,

From a json datafile I iterate over I am trying to captire a key whose value is a string containing xml. This string needs to be converted to json (not a full json file though, just a small piece of json. That piece need to be pasted into the body of a REST request .

It is probably peculiar to have xml in json like this but there are reasons the file looks like this. Below is a anonymized generalized idea of what the data file looks like.

[
{

    "name": "the name",
    "plan_id": "qte46rytrtty7778u",
    "the_piece": "<UserList> <UserIdentification> <UserNumber>6767656</UserNumber> </UserIdentification> </UserList>",
    "custom_fields": "",
  }
]

In the request pre request script. but I ran into problems. This is what I tried.

var the_piece_xml_string = pm.iterationData.get("the_piece");
pm.collectionVariables.set("the_piece", xml2Json(the_piece_xml_string));

And in the Body

{
     "name": "{{thename}}",
     "plan_id": "{{the_plan_id}}",
    "The_Piece": {{the_piece}},
    "CustomFields": {},
}

From the console this is what is added to the actual request body. Not something that fits nicely into json.

"The_Piece": [object Object],

Any help greatly appreciated.

Hi Magnus,

It’s possible that I’m not understanding what’s happening to your XML (especially with your example being generalized) but if I change your collectionVariable setter to use JSON.stringify instead:

var the_piece_xml_string = "<UserList> <UserIdentification> <UserNumber>6767656</UserNumber> </UserIdentification> </UserList>";
pm.collectionVariables.set("the_piece", JSON.stringify(the_piece_xml_string));

…then with the request body that you specified, the resulting value is correctly encoded upon submission -

I’d be interested to know if this works, and if not, whether you’ve got a different problem now :slight_smile:

2 Likes

Hello @neilstudd and thanks for your response.

What I need in the end is an output that has no trace of xml at all. The data structure in the body should be pure json. Generic example below:

{
  "name": "thename",
  "plan_id": "fff8679ok76987",
  "The_Piece": {
    "UserList": {
      "UserIdentification": {
        "UserNumber": "45h5u565h5h"
      }
    }
  },
  "CustomFields": {}
}

I hope that makes sense. It is perhaps strange that I start with having xml in the json in the first place. The reason is that at my company we use the same data file to make soap requests. But in my case I need to use the very same data file and convert a string that contains xml to be used in a REST request.

Ah OK, now I understand what you’re trying to do, thanks!

The library that I’d normally use for this is xml2js; I’d welcome somebody showing me a simpler solution, but in the meantime, this should do what you’re hoping for:

var parseString = require('xml2js').parseString;
parseString(the_piece_xml_string, function (err, result) {
    pm.collectionVariables.set("the_piece", JSON.stringify(result));
});

:tada: Ta-da:

xml2js has other configuration options if you want to more precisely control the output format (which may be required depending on your ‘real’ XML structure) and it’s definitely worth including some error-handling in case the XML data is returned in an unexpected/invalid format, but hopefully this helps you get to the next stage?

1 Like

Thanks a lot @neilstudd . That was spot on. You have 100% answered the question I asked, and I marked your response as the solution. I tried, on my end and it works as described.

The thing is, since I had to be very careful not to show any real examples of the xml, I presented an example that was not completely accurate. In my example above the xml string contains a parent element element and every subsequent element is nested . But the xml I need to pass does not have a top level element.

A more realistic example is this:

"the_piece": "<some_element>content of some element</some_element><UserList> <UserIdentification> <UserNumber>6767656</UserNumber> </UserIdentification> </UserList><another_element>content of another element</another_element>",

At present only the very first element is parsed and that’s it.

"The_Piece": {"SomeElement":"content of some element"},

Is there any way to solve this with the xmltojs library or would I need to change the data file to pass a xml that has a top level element?

d

Hi Magnus,

If you have control over the format of your data file, then I would definitely recommend wrapping the entire thing inside a single top level element. You could possibly apply this string padding within your pre-request script, if the file itself is sacrosanct (as long as you otherwise trust the file structure.)

Strict XML should only ever have a single root node, and the creator of xml2js has explicitly stated this regarding their package.

2 Likes

I do have control of the format of the data file. The piece of xml is passed into a top level element. I could just include that top level element itself in the xml string. Thanks again!

1 Like

Hi Neil.
I’ve tried your solution, but I get an extra {"$": in my result. Do you know why?