简体   繁体   中英

why does my JSON file contain [object Object]?

I am using sqlite3 and nodeJS, and querying a database. I want to copy the queries into a json file. I run into problems with the json file having strings and objects. Why does my JSON file contain:

[object Object]

Here is my code:

db.all(sql, [], (err, rows) => {
if(err) { 
  throw err; 
  }rows.forEach((row) => {
      arr_string = JSON.parse(JSON.stringify(row));
      fs.writeFile("tempo.json", arr_string, function(err){
    });

      console.log(arr_string);
  });
});

I would like to eventually use ajax requests for these entries.

I think while storing JSON data into SQLite, you're not stringifying it. If you directly store JSON data into SQLite it'll store like [object Object] format. My suggestion is to stringify the data while storing in SQLite. And while retrieving only parse it. Then your problem will solve.

arr_string is actually not a string, as you JSON.parse d it. Remove the JSON.parse call.

arr_string = JSON.parse(JSON.stringify(row));
           //^---------^--- This is parsing the string back to a new object.

You are parsing the stringified item to a new Object, hence the node.js file writer is trying to write the object to the write stream, resulting in interpreting it as [object Object] .

Just omit the JSON.parse , so that the stream will effectively be a string instance:

arr_string = JSON.stringify(row);

Additionally, you should aggregate the result and write it only one time, or append to the file:

db.all(sql, [], (err, rows) => {
if(err) { 
  throw err; 
  }
  let _strings = [];
  const newline_separator = ''; // <-- use whatever separator you need.
  rows.forEach((row) => {
      arr_string = JSON.stringify(row);
      console.log(arr_string);
      _strings.push(arr_string);
  });
  fs.writeFile("tempo.json", _strings.join(newline_separator), function(err){});
});

Since it's a json, I would suggest you to provide us a more precise input, so that we can guess what the expected result is.

The JSON.stringify() method converts a JavaScript value to a JSON string

arr_string = JSON.stringify(row);

"the problem now with doing that is now the JSON file only writes the last row (and i queried 4) and also I cannot call arr_string.caphi because its not an object anymore"

Create an empty array(list) and push each result of query in array.And then 
finally convert it into JSON.

sample code :

var rows=[{"holla":"bolla"},{"holla":"bolla1"}];
console.log(JSON.stringify(rows));

JSON.stringify() takes JSON object and returns String
JSON.parse() takes String and returns JSON object

try :

rows.forEach((row) => {
  arr_string = JSON.stringify(row); // 'row' data is converted to String
  fs.writeFile("tempo.json", arr_string, function(err){
});`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM