最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - why does my JSON file contain [object Object]? - Stack Overflow

programmeradmin2浏览0评论

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 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.

Share Improve this question edited Oct 23, 2018 at 11:39 mplungjan 179k28 gold badges182 silver badges240 bronze badges asked Oct 23, 2018 at 11:36 Dark LotusDark Lotus 691 silver badge7 bronze badges 1
  • 3 Do not parse back to object. Simply try arr_string = JSON.stringify(row); – Nikhil Aggarwal Commented Oct 23, 2018 at 11:37
Add a ment  | 

5 Answers 5

Reset to default 3
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.

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.

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){
});`

发布评论

评论列表(0)

  1. 暂无评论