3

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
  • 495
  • 2
  • 6
  • 20

2 Answers2

12

As simple as that:

var json = JSON.stringify({ data: data });
VisioN
  • 138,460
  • 30
  • 271
  • 271
2

Try this

JSON.stringify({'data':data})
Ravi
  • 29,945
  • 41
  • 114
  • 168
Subbu
  • 3,249
  • 4
  • 23
  • 36