roschulze roschulze - 3 months ago 18
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:


But I want this:


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

Answer Source

As simple as that:

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