1

I have an array with objects in Javascript. I want to save this array to a .json file.

Before I added the objects to the file, I console.log the objects.

// Client
Object {id: "1", color: "#00FF00"}
Object {id: "2", color: "#FF7645"}
Object {id: "3", color: "#FF8845"}

Then I post the jsonArray to my nodejs server like this:

// Client
$.post('/savejson', 'json=' + JSON.stringify(jsonArray)) // This works

Catch the post and save the file in nodejs like this:

app.router.post('/savejson', function(data) {
    url = '/jsonfiles/something.json'
    // Nodejs Server
    fs.writeFile(url, data.body.json, function(error) {
        if(error) {
            console.log(error)
            return
    }
    console.log('Saved file: ' + url)
})

Now I have a json file with an array with objects like this:

something.json

[
    {"id":"1","color":"#00FF00"},
    {"id":"2","color":"#FF7645"},
    {"id":"3","color":"#FF8845"}
]

I read the file like this:

// Nodejs Server
jsonfile = fs.readFileSync(url, 'UTF-8')
// Output jsonfile: 
[{"id":"1","color":"#00FF00"},{"id":"2","color":"#FF7645"},{"id":"3","#FF8845"}]

Parse it

// Nodejs Server
jsonArray = JSON.parse(jsonfile)
// Output jsonArray: 
[{id: '1',color: '#00FF00'},{ id: '2',color: '#FF7645'},{ id: '3',color: '#FF8845'}]

And send back to the client

// Nodejs Server
window.newjson(jsonArray)

At my client I catch the file with:

// Client
window.newjson = function(jsonArray) {
    // Here foreach loop
}
// Output jsonArray:
undefined[3]
    0: 
        color: "#00FF00"
        id: "1"
        __proto__: 
    1: 
        color: "#FF7645"
        id: "2"
        __proto__: 
    2: 
        color: "#FF8845"
        id: "3"
        __proto__: 
    length: 3
    __proto__: undefined[0] 

And for each object I console.log the object.

Output

// Client
{id: "1", color: "#00FF00"}
{id: "2", color: "#FF7645"}
{id: "3", color: "#FF8845"}

Noticed the Object word is difference.

Now I want the same file to be saved again like this:

// Client
$.post('/savejson', 'json=' + JSON.stringify(jsonArray)) // Not working anymore...

When I use JSON.stringify(jsonArray) at client side, I get the error: Uncaught illegal access

I also tried to use JSON.parse(jsonArray) at client side, but this one give me the error Uncaught SyntaxError: Unexpected token o

When I log the jsonArray BEFORE the second post:

// 1
console.log(jsonArray)

// Result
Array[3]
    0: 
        color: "#00FF00"
        id: "1"
        __proto__: 
    1: 
        color: "#FF7645"
        id: "2"
        __proto__: 
    2: 
        color: "#FF8845"
        id: "3"
        __proto__: 
    length: 3
    __proto__: Array[0] 


// 2
console.log(jsonArray.toString())

// Result
[object Object],[object Object],[object Object]


// 3
JSON.stringify(jsonArray)

// Result
Uncaught illegal access


// 4
JSON.parse(jsonArray)

// Result
Uncaught SyntaxError: Unexpected token o

What did I wrong? Why I'm missing the Object word?

Ron van der Heijden
  • 14,803
  • 7
  • 58
  • 82
  • 2
    Not much we can do without seeing the code. Sounds like you're calling `JSON.stringify()` and `JSON.parse()` on the wrong things. – Anthony Grist Apr 05 '13 at 12:20
  • I wouldn't want to see anywhere close to all of it; only the parts where you save to, and then read from, your .json file. – Anthony Grist Apr 05 '13 at 12:23
  • Obviously this means that you are doing something wrong when parsing/stringifying. Are you by any chance using `stringify` in a loop? It seems that your objects are actually strings. Have a look at this jsfiddle: http://jsfiddle.net/2z3Qq/1/ I.E. I think `window.newjson` is the culprit. Or maybe the way you pass the data to it is broken. – freakish Apr 05 '13 at 12:50
  • Where are semicolons? – Oleg Apr 05 '13 at 12:53
  • So, show us the code where you create `jsonArray` on NodeJS side, which then you pass to `window.newjson`. – freakish Apr 05 '13 at 12:58
  • @Oleg, they are unnecessary – Ron van der Heijden Apr 05 '13 at 12:59
  • @freakish The jsonArray is created client side, sent by `$.post` to the server and the server sends it back using the `window.newjson` – Ron van der Heijden Apr 05 '13 at 13:06
  • @Bondye http://en.wikipedia.org/wiki/JavaScript_syntax#Whitespace_and_semicolons and http://stackoverflow.com/questions/444080/do-you-recommend-using-semicolons-after-every-statement-in-javascript – Oleg Apr 05 '13 at 13:09
  • 1
    @Bondye Obviously it's impossible, because he cannot pass an object from server to client. He has to stringify it somewhere. The code snippet is incomplete and I have a feeling that there's a mistake there. – freakish Apr 05 '13 at 13:10
  • 1
    @Bondye Where is the code that stringifies `jsonArray` on the server side? – freakish Apr 05 '13 at 13:39
  • 1
    @Bondye You've written that you read the file like this (on the server side): `jsonfile = fs.readFileSync(url, 'UTF-8')` then you parse it (on the server side) `jsonArray = JSON.parse(jsonfile)` and then you send it back to client `window.newjson(jsonArray)`. And I'm asking: where did you stringify `jsonArray`? In other words: how are you sending the data back? P.S. Sorry, for some reason I thought that you're not the OP. :/ – freakish Apr 05 '13 at 13:50
  • I sent the data back to the window (using appjs). `window.newjson` on server side call the function `window.newjson = function()` on client side, I just pass the jsonArray as parameter. So I need to stringify the parameter? – Ron van der Heijden Apr 05 '13 at 13:59
  • @freakish Wow, thats it!! A jsonArray as parameter must be stringified and parsed again ..... – Ron van der Heijden Apr 05 '13 at 14:49

2 Answers2

2

You have to stringify jsonArray before sending it back to the client side.

freakish
  • 54,167
  • 9
  • 132
  • 169
  • 1
    @Bondye No problem. That's a general rule that you have to stringify everything before sending it to the client/server. – freakish Apr 05 '13 at 14:56
0

The unexpected token problem could be because you are calling JSON.parse on a JavaScript Object as mentioned here: d3.js json uncaught syntax error unexpected token o . Then for JSON.stringify(jsonArray) at client side where you get the error "Uncaught illegal access", debug by checkking if jsonArray is null(as null is an object still):

if(variable==null){
// Do stuff console.log etc
}

(although it is very rare for this to be a problem .strigify should print null) If this fails your variable could also be undeclared. I think the problem could be what @Anthony Grist said initially

Community
  • 1
  • 1
Andreas
  • 970
  • 18
  • 28