skip to Main Content

Hey I’m trying to parse a javascript object to xml string in node.js app. I’m using the npm package json2xml like this

json2xml(jsonObject, { header: true });

my issue is that when there’s an array object it will parse it like this:

root: {
    foo: [
        {
            bar: 'a',
        },
        {
            bar: 'b',
        },
    ],
};
<root>
    <foo>
        <bar>a</bar>
        <bar>a</bar>
    </foo>

and not like this:

<root>
    <foo>
        <bar>a</bar>
    </foo>
    <foo>
        <bar>b</bar>
    </foo>
</root>

as I wish it did. Do you have any suggestions or any other libraries I can use to fix this issue?

I’ve tried couple of other libraries: jstoxml, @javadev/xmltojson, fast-xml-parser, that didn’t fix the issue. I’m thinking to write my own jsToXml parser xD

4

Answers


  1. Chosen as BEST ANSWER

    Ok suppose I have a little more complex object like this:

    {
      root: {
        method: "ping",
        success: 1,
        error_code: 0,
        error_text: null,
        params: {
          foo: 11,
          bar: [
            {
              id: 62278639526,
              success: 1,
              error_code: 0
            },
            {
              id: 96176048010,
              success: 1,
              error_code: 0
            }
          ]
        },
        response_id: "b33ba904",
        time: 1681373894,
      }
    }
    

    I haven't found a way how to convert the js object to xml to this:

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
       <method>ping</method>
       <success>1</success>
       <error_code>0</error_code>
       <error_text />
       <params>
          <foo>11</foo>
          <bar>
             <id>62278639526</id>
             <success>1</success>
             <error_code>0</error_code>
          </bar>
          <bar>
             <id>96176048010</id>
             <success>1</success>
             <error_code>0</error_code>
          </bar>
       </params>
       <response_id>b33ba904</response_id>
       <time>1681373894</time>
    </root>
    

    instead I always get this:

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
       <method>ping</method>
       <success>1</success>
       <error_code>0</error_code>
       <error_text />
       <params>
          <foo>11</foo>
          <bar>
             <id>62278639526</id>
             <success>1</success>
             <error_code>0</error_code>
             <id>96176048010</id>
             <success>1</success>
             <error_code>0</error_code>
          </bar>
       </params>
       <response_id>b33ba904</response_id>
       <time>1681373894</time>
    </root>
    

  2. The object should be composed like that:

    {root: [{foo: {bar: "a"}}, {foo: {bar: "b"}}]}
    
    Login or Signup to reply.
  3. it’s not possible to convert your more complex example as you want, unless you add a key in ‘params’, but it’s not the same thing:

    {
       "root":{
          "method":"ping",
          "success":1,
          "error_code":0,
          "error_text":null,
          "params":{
             "foo":11,
             "bar":[
                {
                   "bar":{
                      "id":62278639526,
                      "success":1,
                      "error_code":0
                   }
                },
                {
                   "bar":{
                      "id":96176048010,
                      "success":1,
                      "error_code":0
                   }
                }
             ]
          },
          "response_id":"b33ba904",
          "time":1681373894
       }
    }
    
    Login or Signup to reply.
  4. exactly it’s not the same thing, then I’ll have this:

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
        <method>ping</method>
        <success>1</success>
        <error_code>0</error_code>
        <error_text />
        <params>
            <foo>11</foo>
            <bar>
                <bar>
                    <id>62278639526</id>
                    <success>1</success>
                    <error_code>0</error_code>
                </bar>
                <bar>
                    <id>96176048010</id>
                    <success>1</success>
                    <error_code>0</error_code>
                </bar>
            </bar>
        </params>
        <response_id>b33ba904</response_id>
        <time>1681373894</time>
    </root>
    

    so I’m not sure, if what I want is possible with this library. I just wanted to make sure if there’s any solution. If not it’s okay and I can find some other way.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search