12

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.

reZach
  • 8,945
  • 12
  • 51
  • 97
  • how do you check the output of JSON.stringify ? – mathieu Apr 12 '15 at 04: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: http://stackoverflow.com/questions/8630471/strings-as-keys-of-array-in-javascript – Alejandro Iván Apr 12 '15 at 04:12
  • The result of the JSON.stringify is sent to a NodeJS server where I log the object (prints it) – reZach Apr 12 '15 at 04: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. – mathieu Apr 12 '15 at 04:13
  • Sorry, your input object is incorrect, check hamed answer. – mathieu Apr 12 '15 at 04:20
  • @mathieu, the problem stemmed from my server. I was grabbing the wrong information. JSON.stringify does correctly parse through my array – reZach Apr 12 '15 at 04:27
  • Ok then i was right although i was wrong :D – mathieu Apr 12 '15 at 04:32
  • what about if the `data` array contain more multiple item? you can't use `data: {}` anymore, it should `data: ['id':1],['id':2]`. I encountered this kind of problem. – bossajie Apr 23 '19 at 06:48
  • @ajiejot that data looks like invalid json to begin with. `data` cannot be an array of arrays, it it _is_, then the code looks like this: `data: [{'id':1}, {'id':2}]` – reZach Apr 25 '19 at 14:11

3 Answers3

11

I think your data array should be like this:

var data = [{
name: string,
active: bool,
data: { //Use {} instead of []
    value: number,
    date: string
  }
}]
hamed
  • 7,939
  • 15
  • 60
  • 114
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

L J
  • 111
  • 1
  • 3
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"]}]
Eric
  • 16,397
  • 8
  • 68
  • 76