MauirceA
MauirceA

Reputation: 321

How to create JSON in Javascript with a variable?

I have code that returns an array in json format but I want it to be under "data": So right now, it shows as:

"["newDate": 'test'": 'test',"attendees": 'test,"name": 'Foo']"

How do I get it as: "{ data: ["newDate": 'test'": 'test',"attendees": 'test,"name": 'Foo'] }"

let countries = [];
    for (x in newObj) {
        countries.push({
            "newDate": newObj[x][0],
            "count": getDates()[x].length,
            "attendees": getDates()[x],
            "name": x
        })
    }
return JSON.stringify(countries);

Upvotes: 0

Views: 151

Answers (2)

smurfo
smurfo

Reputation: 91

JSON.stringify({data:countries});

Upvotes: 1

BotNC
BotNC

Reputation: 56

You need to use this argument:

JSON.stringify({ data: countries });

Upvotes: 0

Related Questions