-3

I would like to create a json object such as this that I can send to the server:

{
  "email": "[email protected]",
  "profile": {
          "token": "test"
  }
}

I can create the JSON but don't know how to create one that has multiple objects like the one above. This is what I've done so far

(In Console)

> var dataModel = { email: "[email protected]", token: "sometoken"}
>  undefined
> dataModel
>  Object {email: "[email protected]", token: "sometoken"}

The token needs to be inside profile

1
  • Should be noted that what you have posted in your top 'code-box' IS JSON. JSON (JavaScript Object Notation) is a literal way of writing JS objects. The JSON 'string' is essentially a JS object being declared without the var myVar = bit. Commented Mar 19, 2015 at 16:31

4 Answers 4

2

Most modern browsers have JSON.stringify(yourData) and JSON.parse(jsonData);

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

Comments

1

Just create your object as a plain JavaScript object:

var object = {
  email: "[email protected]",
  profile: {
    token: "test"
  }
}

Then convert it to JSON:

var json = JSON.stringify(object);

Comments

0
var dataModel = {
    email: "[email protected]",
    profile: {
        token: "sometoken"
    }
};
var dataModelJSON = JSON.stringify(dataModel);

Supported by main browsers: http://caniuse.com/#search=JSON.stringify

Comments

-1

try:

var dataModel = { email: "[email protected]"};
dataModel.profile = {token : "test"}

3 Comments

That will throw an 'undefined' exception.
Not acquainted with the {token = "test"} syntax. Is that something new?
Nope, not new, now fixed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.