14

I am currently in investigation why JSON.stringify() does not properly parse my object. This is my object I am trying to parse into a JSON string:

var data = [{
    name: string,
    active: bool,
    data: [
        value: number,
        date: string
    ]
}]

However when calling JSON.stringify() on my object, I get a result similar to this:

/* JSON.stringify(data) */
[{
    name: string,
    active: bool,
    data: [
        [Object],
        [Object],
        ...
    ]
}]

Is there a nuance to JSON.stringify that causes this to happen? I'd be happy to add more details to my question if it helps clarify any more details.

9
  • how do you check the output of JSON.stringify ? Commented Apr 12, 2015 at 4:11
  • 1
    Did you try declaring data as an object instead of an array? data: { ... } (you're using strings instead of integer indexes). Look at this answer: stackoverflow.com/questions/8630471/… Commented Apr 12, 2015 at 4:12
  • The result of the JSON.stringify is sent to a NodeJS server where I log the object (prints it) Commented Apr 12, 2015 at 4:12
  • I guess your object is correctly serialized, the problem occurs later. Check (spy) the data transmission between your browser and the server to confirm this. Commented Apr 12, 2015 at 4:13
  • Sorry, your input object is incorrect, check hamed answer. Commented Apr 12, 2015 at 4:20

3 Answers 3

11

I think your data array should be like this:

var data = [{
name: string,
active: bool,
data: { //Use {} instead of []
    value: number,
    date: string
  }
}]
Sign up to request clarification or add additional context in comments.

Comments

8

You can actually use the second argument to JSON.stringify. Two options for this, you can either specify all the prop names you want stringified:

var data = [{
name: string,
active: bool,
data: [
    {value: number},
    {date: string}
  ]
}]

JSON.stringify(data, ['name', 'active', 'data', 'value', 'date'])

=> '[{
  "name":"string",
  "active":"bool",
  "data":[
    {"value":"number"},
    {"date":"string"}
  ]}
]'

Or use a replacer function with the same result:

JSON.stringify(data, function replacer(key, value) { return value})

=> '[{
  "name":"string",
  "active":"bool",
  "data":[
    {"value":"number"},
    {"date":"string"}
  ]}
]'

Original source: https://javascript.info/json

Comments

1

You need to change the value of data into a JSON object instead of a JSON array to make it work.

JSON.stringify() seems to parse it without any issues:

working example:

var o1 = [{
    "name":"string",
    "active":"bool",
    "data":{
        "value":"number",
        "date":"string"
    }
}];
var o2 = [{
    "name":"string",
    "active":"bool",
    "data":[
        "value",
        "number",
        "date",
        "string"
    ]
}];

console.log(JSON.stringify(o1)); // outputs: [{"name":"string","active":"bool","data":{"value":"number","date":"string"}}]
console.log(JSON.stringify(o2)); // outputs: [{"name":"string","active":"bool","data":["value","number","date","string"]}]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.