roschulze roschulze - 1 month ago 10
Javascript Question

JSON.stringify set root element

I want to convert my object into a JSON String where the root element should be the name of my object.

var data = {
name: 'qwertz',
age: 23,
skills: [
'html', 'css'
]
}

var json = JSON.stringify(data);


The output is:

{"name":"qwertz","age":23,"skills":["html","css"]}


But I want this:

{"data":{"name":"qwertz","age":23,"skills":["html","css"]}}


Can someone give me a hint how to reach this? Thanks you :)

Answer Source

As simple as that:

var json = JSON.stringify({ data: data });