Efficiently merging strings and arrays to create flawless JSON data

After fetching data from an XML document and storing them in three variables - the root element name, an array containing all the names of the root's children, and a second array with the length of those children sub-nodes, I am trying to convert these variables into a JSON object in the following format:

{ "root_name": {
         "childName[0]": "lengthSubNodes[0]",
         "childName[1]": "lengthSubNodes[1]",
         "childName[2]": "lengthSubNodes[2]"
}  

using this function:

function XMLtoJSON(rootName, childNames, childNumbers){
            var xmlObject = {};
            xmlObject[rootName] = {}; 
            for(var i = 0; i < childNames.length; i++ ){
                xmlObject[rootName][childNames[i]] = childNumbers[i];
            }

        }

While everything works correctly, I encounter an issue when dealing with an XML document that has multiple children with the same name and length. They appear only once like this:

{ "catalog": {
         "book": 6
}

instead of how it should look like:

{ "catalog": {
         "book": 6,
         "book": 6,
         "book": 6
}  

Is there a way to address this problem?

Answer №1

When working with XML and JSON, it's important to note that multiple keys with the same name under one parent are not allowed. Additionally, the order of nodes in XML is significant, which can be lost when converting to a JavaScript map. To create a utility for converting XML to JSON, you may need to develop your own JSON schema. Here is an example:

{
    name:      "catalog",
    value:     null,
    children: [
        {
            name: "book",
            value: 6
        },
        {
            name: "book",
            value: 6
        },
        {
            name: "book",
            value: 6
        },
    ]
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The functionality of JavaScript on an icon that is supposed to expand and collapse a table is not functioning properly when the enter

On this particular page, I have a toggleable table of information that expands and collapses when the user clicks on a clickable +/- icon. However, there seems to be an issue where if the user tabs to the icon and tries to expand/collapse it by pressing en ...

Rapidly typing text into a text box using Selenium and Python

Currently, I am utilizing Selenium in conjunction with Python (Chrome driver) to populate text boxes. However, the process is taking longer than desired due to the presence of numerous textboxes. My solution involves using a series of driver.find_elemen ...

Dealing with empty POST request bodies in Node.js Express is a common challenge

In my Node.JS project using Express framework, I am encountering an issue while trying to access the body of a POST request. The POST request is being sent through an HTML form, and upon checking with WireShark, I can see that the request is indeed getting ...

Learning how to access my CSS file using Express and Node.js

I am new to using express and node.js. I am trying to develop my app, but for some reason, my style.css file is not being recognized and I am unsure why. Initially, I attempted to use .scss files, but after researching, I discovered that it was not possi ...

JSON.Net appears to incorrectly validate a schema using anyOf in cases where it should not

My current task involves detecting whether the user has input a boolean as a string rather than an actual boolean value. To test this, I am examining the commentsModule/enabled property to see if the value is false, once surrounded by quotes and once witho ...

An issue occurred while running the "gulp test" command due to a problem with the 'gulp-tslint-log'

Having issues running gulp on a JHipster generated project. I've already installed all node_modules using npm install and here are my package.json and gulpfile.js: { "name": "myapp", "version": "0.0.0", "description": "Description for myapp", ...

Unable to display shadows in Three.js

I've hit a roadblock with the shadow effect at the moment, despite trying various solutions for hours, it's still not appearing. Can anyone point out what I may have done incorrectly? Below is the code snippet for my current scene: //Setting up ...

Enforcing static array bounds in C++

I am currently seeking guidance on a question for my programming languages course that involves implementing an array class in C++ with statically checked overflow for element access. We are forbidden from using C++11 (static assertion) or any other pre-ma ...

Searching for the "MongoDB" object using Node.js to execute an array query

[ { "_id" : ObjectId("5abce64d86d3289de052a639"), "username" : "doggy", "email" : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2a4e454d4d536a594b484b4449435f44435c044f4e5f">[email protecte ...

What is the best way to ensure that a link fragment scrolls to the top of the page in Angular?

Having trouble with link fragments in my Angular single-page-app: <a href="/#/search">Search</a> When clicking this link, it takes me to the right page but keeps my scroll position. I want it to scroll to the top of the page so I'm curre ...

Is it possible to utilize xmlhttprequest.open and xmlhttprequest.send to create a new browser window?

I am in need of a pop-up editor that allows users to choose what to edit, with the ID being sent to a new window. In this new window, data will be retrieved from a database, displayed for editing, and changes can be saved. Once the edits are completed, the ...

What is the best way to choose dropdown values by utilizing various button IDs?

I have four different vacation destinations and four buttons. I want to automatically select each destination separately when the corresponding button is clicked. <select class="aa" required="" name="f1990" {input.multiple}="" {input.size}="" id="f19 ...

Is it acceptable in the C programming language to utilize pointers with arrays of incomplete types?

While the IAR Embedded C compiler accepts this code without issue, gcc and clang run into problems with it: struct incomplete; typedef struct incomplete (*why_not)[2]; struct incomplete {struct incomplete *known_to_work;} array[2]; why_not ok = &array ...

Learn how to import from a .storybook.ts file in Vue with TypeScript and Storybook, including how to manage Webpack configurations

I'm currently utilizing Vue with TypeScript in Storybook. Unfortunately, there are no official TypeScript configurations available for using Vue with Storybook. How can I set up Webpack so that I am able to import from another .storybook.ts file with ...

List of Tabs Displayed in the Scroll Bar

I'm currently facing an issue with JavaScript tabs on my website. The problem is that the tab names are getting included in the scrollbar, causing them to disappear when users scroll down. I have shared the code on JSFiddle for reference (link provide ...

invoke the call_user_func_array function with extra arguments for the specified method

I have the following code in my App.php file: When I visit a URL like this: http://mywebroot/myapp/param1/param2 # anything leftover in $url, set as params, else empty. $this->params = $url ? array_values($url) : []; print_r($this->params); // Thi ...

Unable to verify the absence of the JSON key value in PHP

I require assistance. I am trying to verify if the key value is present in the JSON array using PHP, but encountering warning messages and no values are being returned. Below is a breakdown of my code: $mainArr=array(array("type"=>1,"name"=>"hello") ...

Is there a way to automatically close all open sub-menus when clicking on a different parent menu item?

Check out this LINK for the code I am using. When I click on the Parent Menu, such as Services, the sub-menu of the Services menu will open. However, when I click on another menu, the sub-menu will also open. I want the previous sub-menu to close when I c ...

Leverage the JSON Input step for handling irregular data structures

Trying to process the following JSON data using a JSON Input step: {"address":[ {"AddressId":"1_1","Street":"A Street"}, {"AddressId":"1_101","Street":"Another Street"}, {"AddressId":"1_102","Street":"One more street", "Locality":"Buenos Aires"}, ...

A coding algorithm for determining text similarity percentage by calculating the edit distance

I have a good understanding of various edit-distance algorithms in JavaScript, but my goal is to calculate text similarity as a percentage based on them. Can anyone provide guidance on how to implement this feature? ...