1

In PHP when I want to give a name to an array of json objects I use this line.

$json = json_encode(array("users" => $output));

And this variable would be printed like this.

{"users":[{"user_id":"1"}

But now, I'm building a project in Node.js and I need to give a name to an array, and I don't know how to do it, I'm printing data using this line:

 res.send(JSON.stringify(users, null, 4));

[ { "user_id": "1" }, { "user_id": "2" } ]

Thank you.

0

3 Answers 3

2

Just build the object with "root" key users before stringify

res.send( JSON.stringify( { 'users': users } , null, 4) );
Sign up to request clarification or add additional context in comments.

Comments

1

You can send json directly specifying data points

res.json({ variablename : thevalue (in your case array)})

if more than one value, just follow the syntax

res.json({ variablename : thevalue1, variablename : value2)})

Comments

-1

You can something like this:

 {'users': JSON.stringify(users, null, 4)}

1 Comment

Please explain for voting down, would help me and others too.

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.