2

How can I change this:

[{"name": "Donald"}, {"name": "George"}]

To this:

{MyArray: [{"name": "Donald"}, {"name": "George"}]}

This is for a database server I made using node.js, express and body-parser. The string is produced using the collection('name').find().toArray function.

2 Answers 2

3

You could convert the string to an object and use an object with the named property. Stringify the object to a string.

var json = '[{"name": "Donald"}, {"name": "George"}]',
    object = { myArray: JSON.parse(json) };
    
console.log(JSON.stringify(object));

Sign up to request clarification or add additional context in comments.

1 Comment

In case you want to work with the key even before you send (or parse it), here's the working fiddle: jsfiddle.net/9rx1s1t6
0

Like this:

var arr = [{"name": "Donald"}, {"name": "George"}]
var obj = {}
obj.MyArray = arr;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.