Skip to content
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

Handling array items #31

Open
jlguenego opened this issue Apr 30, 2018 · 3 comments
Open

Handling array items #31

jlguenego opened this issue Apr 30, 2018 · 3 comments

Comments

@jlguenego
Copy link

I need to convert the following json into xml:

{
    "content": [
        {
            "_id": "012345678901234567890123",
            "lastname": "Guénégo",
            "firstname": "Jean-Louis"
        },
        {
            "_id": "5ae5ef542523d439d8083335",
            "lastname": "Phengsiaroun",
            "firstname": "Dany",
            "__v": 0
        }
    ]
}

The output does not satisfy because it does not clearly sepearate the array items:

<content>
    <_id>012345678901234567890123</_id>
    <lastname>Guénégo</lastname>
    <firstname>Jean-Louis</firstname>
    <_id>5ae5ef542523d439d8083335</_id>
    <lastname>Phengsiaroun</lastname>
    <firstname>Dany</firstname>
    <__v>0</__v>
</content>

I would suggest to add in the options something like:
handleArrayElement: true

that would produce:

<content>
    <item>
        <_id>012345678901234567890123</_id>
        <lastname>Guénégo</lastname>
        <firstname>Jean-Louis</firstname>
    </item>
    <item>
        <_id>5ae5ef542523d439d8083335</_id>
        <lastname>Phengsiaroun</lastname>
        <firstname>Dany</firstname>
        <__v>0</__v>
    <item>
</content>
@jlguenego
Copy link
Author

jlguenego commented Apr 30, 2018

In fact it is a nice to have because I can quickly solve it outside of the jsontoxml lib.

function handleArray(js) {
	if (js instanceof Array) {
		return js.map(n => ({item: n}));
	}
	if (js instanceof Object) {
		const result = {};
		for (let p in js) {
			result[p] = handleArray(js[p]);
		}
		return result;
	}
	return js;
}

@MahmoudYaser1
Copy link

in my case i want to embed array items without array key .. for example
this is before

<content>
    <item>
        <lastname>Guénégo</lastname>
        <firstname>Jean-Louis</firstname>
    </item>
    <item>
        <lastname>Phengsiaroun</lastname>
        <firstname>Dany</firstname>
    <item>
</content>

just without the array key

<item>
        <lastname>Guénégo</lastname>
        <firstname>Jean-Louis</firstname>
    </item>
    <item>
        <lastname>Phengsiaroun</lastname>
        <firstname>Dany</firstname>
<item>

@ConPac
Copy link

ConPac commented Feb 26, 2019

Hi @jlguenego:

Just run into this issue today, until I realized from the example in readme that all that's needed is a new object with name and children properties:

let jsonObject = JSON.parse('{"content": [ ... ]}')
let arrayObject = {}
arrayObject.content = jsonObject.content.map(o => Object.assign({name: 'item', children: o})
jsontoxml(arrayObject)

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

No branches or pull requests

3 participants