Skip to content

list of children nodes should be combined under a single parent node (builder) #748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
amitguptagwl opened this issue May 12, 2025 · 2 comments
Labels

Comments

@amitguptagwl
Copy link
Member

Discussed in #552

Originally posted by Effodero March 1, 2023
I have the following JSON

{
	"ExternalId": "123456-01",
	"Name": "Product name",
	"Attributes": [
		{
			"Attribute": {
				"@_id": "FAMILY",
				"Value": "family "
			}
		},
		{
			"Attribute": {
				"@_id": "EXPAND",
				"Value": "expanded-family"
			}
		}
	]
}

and trying to produce this output

<Product>
	<ExternalId>123456-01</ExternalId>
	<Name>Product name</Name>
	<Attributes>
		<Attribute id="FAMILY">
			<Value>123456</Value>
		</Attribute>
		<Attribute id="EXPAND">
			<Value>123456-01</Value>
		</Attribute>
	</Attributes>
</Product>

I've tried different settings, formatted JSON in couple of different ways, but keep getting

<Product>
	<ExternalId>123456-01</ExternalId>
	<Name>Product name</Name>
	<Attributes>
		<Attribute id="FAMILY">
			<Value>123456</Value>
		</Attribute>
	</Attributes>
	<Attributes>
		<Attribute id="EXPAND">
			<Value>123456-01</Value>
		</Attribute>
	</Attributes>
</Product>

I need to format the "Attributes" section as per the first snippet. Is this possible? Is there a specific way to prefix the JSON properties?

@Labmember7
Copy link

Labmember7 commented May 13, 2025

The issue here is that the JSON structure is being interpreted in a way that creates multiple elements instead of grouping all elements under a single parent. To achieve the desired XML output, you need to adjust the JSON structure or configure the XML builder to handle the grouping correctly.

Solution
You can achieve the desired output by ensuring that the Attributes key in your JSON is treated as a single parent element containing multiple Attribute child elements. Here's how you can do it:

Adjusted JSON Input
Modify your JSON structure slightly to ensure that all Attribute elements are grouped under a single Attributes element:

{
    "ExternalId": "123456-01",
    "Name": "Product name",
    "Attributes": {
        "Attribute": [
            {
                "@_id": "FAMILY",
                "Value": "123456"
            },
            {
                "@_id": "EXPAND",
                "Value": "123456-01"
            }
        ]
    }
}

Also ensure that the parser is configured to handle arrays properly. Use the arrayMode option to specify how arrays should be handled. For example:


const json = {
    ExternalId: "123456-01",
    Name: "Product name",
    Attributes: {
        Attribute: [
            { "@_id": "FAMILY", Value: "123456" },
            { "@_id": "EXPAND", Value: "123456-01" }
        ]
    }
};

const options = {
    format: true, // Pretty print the XML
    ignoreAttributes: false, // Include attributes in the output
    arrayNodeName: "Attribute" // Ensure array elements are grouped correctly
};

const builder = new XMLBuilder(options);
const xml = builder.build(json);

This will give you the exepcted output!

@amitguptagwl
Copy link
Member Author

I missed to notice that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants