question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ArrayMode Support

See original GitHub issue

Array mode is the most wanted feature in XML to JSON parser. Check the example to understand the need;

<report>
        <store>
            <region>US</region>
            <inventory>
                <item>
                    <name>Banana</name>
                    <count>200</count>
                </item>
                <item>
                    <name>Apple</name>
                    <count>100</count>
                </item>
            </inventory>
        </store>
        <store>
            <region>EU</region>
            <inventory>
                <item>
                    <name>Banana</name>
                    <count>100</count>
                </item>
            </inventory>
        </store>
    </report>

Parsed response

{
  "report": {
    "store": [
      {
        "region": "US",
        "inventory": {
          "item": [
            {
              "name": "Banana",
              "count": "200"
            },
            {
              "name": "Apple",
              "count": "100"
            }
          ]
        }
      },
      {
        "region": "EU",
        "inventory": {
          "item": {
            "name": "Banana",
            "count": "100"
          }
        }
      }
    ]
  }
}

When the occurence of a tag is 1 it is parsed as an objectg otherwise array. In this case, user need an extra check to traverse through the parser response to confirm if he value of a tag is array type or object type.

I have solved this problem by introducing arrayMode option.

const jsObj = parser.parse(xmlData, {
      arrayMode: true
});

When arrayMode: true, it parses all tags as array if they have nested tags.

{
            "report": [
                {
                    "store": [
                        {
                            "region": "US",
                            "inventory": [
                                {
                                    "item": [
                                        {
                                            "name": "Banana",
                                            "count": 200
                                        },
                                        {
                                            "name": "Apple",
                                            "count": 100
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "region": "EU",
                            "inventory": [
                                {
                                    "item": [
                                        {
                                            "name": "Banana",
                                            "count": 100
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }

When arrayMode: 'strict', it parses all tags as array.

 {
            "report": [
                {
                    "store": [
                        {
                            "region": ["US"],
                            "inventory": [
                                {
                                    "item": [
                                        {
                                            "name": ["Banana"],
                                            "count": [200]
                                        },
                                        {
                                            "name": ["Apple"],
                                            "count": [100]
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "region": ["EU"],
                            "inventory": [
                                {
                                    "item": [
                                        {
                                            "name": [ "Banana" ],
                                            "count": [100]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }

It’ll look like as follow in case of node text, attribute, and CDATA

"item": [
    {
        "#text": "some item detail",
        "@_grade": "A",
        "name": "Banana",
        "count": 200
    },
    {
        "@_grade": "B",
        "__cdata": "som text",
        "name": "Apple",
        "count": 100
    }
]

or in strict mode

"item": [{
    "#text": [ "some item detail"  ],
    "@_grade": [ "A"  ],
    "name": [ "Banana"  ],
    "count": [ 200  ]
},{
    "@_grade": [ "B"  ],
    "__cdata": [ "som text"  ],
    "name": [ "Apple"  ],
    "count": [ 100 ]
    }
]

Please let us know if you think that the parsing output should be different.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jytecommented, Oct 9, 2019

It worked well for my 800MB file, thx 😃

0reactions
amitguptagwlcommented, Nov 28, 2022

@LihiJosef Try isArray option of FXPv4.

isArray: (name, jpath, isLeafNode, isAttribute) => { 
        if( name === "inventory") return true;
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

ArrayMode (POI API Documentation) - Apache POI
Interface for those functions that evaluate arguments in array mode depending on context. Skip navigation links. Overview · Package; Class; Use · Tree ......
Read more >
parsing a particular tag as array using fast-xml-parser in NodeJs
I used this arrayMode: tagName => ['Item', 'Transaction'].includes(tagName) and it worked. – ShailendraChoudhary. Apr 23, 2021 at 6:34.
Read more >
CData JDBC Driver for Domino - ArrayMode - CData Software
CData JDBC Driver for Domino - RSBDomino - ArrayMode: How fields that contain array values are presented.
Read more >
What is Panel Array Mode | ATEN Corporate Headquarters
Manage multiple over IP servers on one screen with ATEN's Panel Array Mode™. The technology now provides capabilities in production line, surveillance. banner....
Read more >
Interface ArrayMode - Adobe Developer
Overview · Package · Class · Deprecated · Help.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found