Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.parse converting to a string instead of passing into array

Yallo, I am at ends as to why notes = JSON.parse(notesString) is converting my array to a string instead of passing my json strings into the array. I tested by checking the typeof before and after. I understand why push cannot be used because it's no longer an array. but I don't know the solution.

Code

// array to store notes in
var notes = [];

// note JSON object
var note = {
    title: title,
    body: body
};

try {
    // read pre-existing content of notes-data.json file
    var notesString = fs.readFileSync('notes-data.json');

    // store pre-existing data as the notes array, passing as 
    // JSON
    console.log("notesString: " + typeof notesString)
    console.log("notes before parse: " + typeof notes)

    notes = JSON.parse(notesString)

    console.log("notes after parse:" + typeof notes)
} catch (e) {

}

// add note to notes array
notes.push(note)

// store content of notes array in notes-data.json as a string 
fs.writeFileSync('notes-data.json', JSON.stringify(notes));

This is my JSON

"[{\"title\":\"herp\",\"body\":\"derp\"},{\"title\":\"herp\"‌​‌​,\"body\":\"derp\"‌​}]‌​"

Output

notesString: object
notes before parse: object
notes after parse:string
C:\Visual Studio 2015\Projects\Note_App_NodeJS\Note_App_NodeJS\notes.js:32
    notes.push(note)
          ^

TypeError: notes.push is not a function

Resolved sorry people I don't know what was going on but I should have validated my output/input first. I don't know why it was formatted in that manner and it's formatted in the correct json format since, when converting to stingify then parsing back. I am using Visual Studio with the Nodejs extension so perhaps that had something to do with it.

like image 733
gxminbdd Avatar asked Dec 20 '25 00:12

gxminbdd


2 Answers

It is a string because of the outer quotes. If you remove those, your JSON is not valid. You must format it according to the JSON rules. All the keys must be strings, and values can be only primitives, like strings, numbers, booleans, arrays or other JSON objects.

Format your JSON like

[
    {
        "title": "herp",
        "body":"derp"
    },
    {
        "title":"herp"‌​‌​,
        "body":"derp"‌
    ​}
]‌​

Here you can see some examples: http://json.org/example.html

like image 70
germanfr Avatar answered Dec 21 '25 12:12

germanfr


sorry I should have been more specific at the momment it contains

"[{\"title\":\"herp\",\"body\":\"derp\"},{\"title\":\"herp\"‌​,\"body\":\"derp\"}]‌​"

That is the JSON expression of a string, which is why you get a string when you parse it.

The string happens to contain a nested set of JSON which is the array you are looking for.

Extract that array from the string and put that in the file.

[{"title":"herp","body":"derp"},{"title":"herp"‌​,"body":"derp"}]‌​
like image 45
Quentin Avatar answered Dec 21 '25 14:12

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!