Vinh Nguyen Le Xuan
Vinh Nguyen Le Xuan

Reputation: 11

In nodejs, when use json.stringify() -> error

I've json object ( this json object returned from database):

{ created_at: 'Wed Nov 21 19:28:24 +0000 2012', id: 271334264140869630, id_str: '271334264140869633', }{ created_at: 'Wed Nov 21 19:28:24 +0000 2012', id: 271334264140869630, id_str: '271334264140869633', }

When I've use JSON.stringify(obj) ->

{ "created_at": "Wed Nov 21 19:28:24 +0000 2012", "id": "271334264140869630", "id_str": "271334264140869633" }{ "created_at": "Wed Nov 21 19:28:24 +0000 2012", "id": "271334264140869630", "id_str": "271334264140869633" }

This obj invalid on http://jsonlint.com/, because error:Expecting 'EOF', '}', ',', ']'

Pls help me fix this error.

Upvotes: 0

Views: 666

Answers (1)

Umesh Patil
Umesh Patil

Reputation: 10705

Below is your valid JSON.

[
    {
        "created_at": "WedNov2119: 28: 24+00002012",
        "id": 271334264140869630,
        "id_str": "271334264140869633"
    },
    {
        "created_at": "WedNov2119: 28: 24+00002012",
        "id": 271334264140869630,
        "id_str": "271334264140869633"
    }
]
  1. If multiple objects are present in parent Object, it forms an array and should be separated by comma ",". The array should be wrapped with squre brackets "[]"
  2. The Id of an object must be string, for being validated by jsonlint. If value is string, quote it with double quotes "".

This is the jsfiddle to test stringify operation

Upvotes: 1

Related Questions