简体   繁体   English

如何 select 并比较 json 文件的 id 并为 Z28A3689BE95C8ABDD84ZE 中的每个结果编写新的 json 文件

[英]How to select and compare id's of a json file and write new json files for each result in node.js

I have a JSON file with two kinds of arrays, namely Persons and Cars.我有一个 JSON 文件,其中包含两种 arrays,即人员和汽车。 Each person has an ID and with each person they have a car or multiple cars.每个人都有一个 ID,每个人都有一辆或多辆汽车。 Each kind of car has also an ID.每种汽车也有一个ID。 First I want to create a new directory.首先,我想创建一个新目录。 In that directory, I want to create new JSON files named (first_name and last_name).在该目录中,我想创建名为(first_name 和 last_name)的新 JSON 文件。 In that file I want to select the person and the car with the same id and store them in the JSON file.在该文件中,我想 select 人和汽车具有相同的 id 并将它们存储在 JSON 文件中。 I don't want to use loops.我不想使用循环。 I can create the directory but the JSON files are not being created.我可以创建目录,但没有创建 JSON 文件。

An example of how json file looks like: json 文件的示例如下所示:

JSON file: JSON 文件:

{
  "persons": [{
    "id": 1,
    "first_name": "Jos",
    "last_name": "Vertommen",
    "birth_day": "1974-03-23",
    "gender": "M",
    "married": true,
    "image": "https://via.placeholder.com/200x80.png?text=Jos",
    "yearsService": 12
  }],
  "cars": [{
    "id": 1,
    "license": "1-AAA-123",
    "brand": "Opel",
    "model": "Zafira",
    "personId": 1
  }]

node.js file node.js 文件

'use strict';
let fs = require("fs");
let jsondata = require("../persons.json");
fs.mkdtemp("cars", (err) => {
  if (err) throw err;
  fs.readFile('../persons.json', 'utf-8', (err, data) => {
    if (err) throw err;
    jsondata = JSON.parse(data);
    let persons = jsondata.persons;
    let cars = jsondata.cars;

    if (persons.id === cars.id) {
      jsondata = JSON.stringify(data)
      let naam = (persons["first_name"] + "_" + persons["last_name"]).toString();
      fs.writeFile(`cars/${naam}.json`, jsondata, () => {})
    } else {
      console.log(err);
    }
  })
});

I idk your file system and your code..我 idk 你的文件系统和你的代码..

I've just created following example on github我刚刚在github上创建了以下示例

in the same directory with code i added a persons.json file which like following.在与代码相同的目录中,我添加了一个persons.json文件,如下所示。

{
  "persons": [
    {
      "id": 1,
      "first_name": "Jos",
      "last_name": "Vertommen",
      "birth_day": "1974-03-23",
      "gender": "M",
      "married": true,
      "image": "https://via.placeholder.com/200x80.png?text=Jos",
      "yearsService": 12
    },
    {
      "id": 2,
      "first_name": "Halil",
      "last_name": "Cakar",
      "birth_day": "1992-09-02",
      "gender": "M",
      "married": false,
      "image": "https://via.placeholder.com/200x80.png?text=Halil",
      "yearsService": 12
    }
  ],
  "cars": [
    {
      "id": 1,
      "license": "1-AAA-123",
      "brand": "Opel",
      "model": "Zafira",
      "personId": 1
    },
    {
      "id": 2,
      "license": "1-AAA-123",
      "brand": "Opel",
      "model": "Zafira",
      "personId": 1
    },
    {
      "id": 3,
      "license": "1-AAA-123",
      "brand": "Opel",
      "model": "Zafira",
      "personId": 2
    }
  ]
}

"use strict";
let fs = require("fs");

fs.readFile("./persons.json", "utf-8", (err, data) => {
  if (err) throw err;
  data = JSON.parse(data);
  let persons = data.persons;
  let cars = data.cars;
  // for every person in this persons array
  for (let i = 0; i < persons.length; i++) {
    // we are getting person
    let person = persons[i];
    // filtering every car he have!
    let personCars = cars.filter((ch) => ch.personId === person.id);
    // create file name
    let filename = `${person.first_name.toLowerCase()}_${person.last_name.toLowerCase()}.json`;
    // i'm just gonna assume that u want to create your files stringified again so
    // create person's json data
    let data = JSON.stringify({ person, cars: personCars });
    // so here we are assuming again that u have a cars/ directory in the same path
    fs.writeFile(`./cars/${filename}`, data, () => {
      // i'm not sure what to use for this maybe consoling every person's file created!
      console.log(person.first_name, " file created!");
    });
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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