4

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 :)

roschulze
  • 505
  • 2
  • 6
  • 21

2 Answers2

13

As simple as that:

var json = JSON.stringify({ data: data });
VisioN
  • 143,310
  • 32
  • 282
  • 281
2

Try this

JSON.stringify({'data':data})
Ravi
  • 30,829
  • 42
  • 119
  • 173
Subbu
  • 3,299
  • 5
  • 24
  • 36