1
1
13-Floky2018-07-21 15:53:11
JSON
13-Floky, 2018-07-21 15:53:11

How to write specific content to Json file?

Here is my code that displays the content of profi.json
And below is the content of Json The
question is how to display not all the contents, but only for example "shopperName": "Vanya Ivanov",
And write to another DATA.json file
And the question is different
How to read several JSON files and after not overwriting in DATA.json, namely adding to the same file

var fs = require('fs');
var file = __dirname + '/profi.json';

fs.readFile(file, 'utf8', function (err, data) {
    
  if (err) {
    console.log('Error: ' + err);
    return;
  }
  

data = JSON.parse(data);
console.dir(data);
});

{
  "orderID": 12345,
  "shopperName": "Ваня Иванов",
  "shopperEmail": "[email protected]",
  "contents": [
    {
      "productID": 34,
      "productName": "Супер товар",
      "quantity": 1
    },
    {
      "productID": 56,
      "productName": "Чудо товар",
      "quantity": 3
    }
  ],
  "orderCompleted": true
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
msdos-x86, 2018-07-22
@msdos-x86

console.log(data[shopperName]);
// Another json file
data2 = JSON.parse(data2)
data2[shopperName] = data[shopperName]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question