Reputation: 15070
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:
[
{"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.
// 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?
Upvotes: 1
Views: 2504
Reputation: 56467
You have to stringify jsonArray
before sending it back to the client side.
Upvotes: 2
Reputation: 1150
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
Upvotes: 0