0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Nodeのfsモジュールでjsonを条件分けして出力する

Posted at

##やりたいこと
・jsonを条件別に分割して出力したい。

##やったこと
・node.jsのfsモジュールを使う。
・今回はJsonPlaceholderのuser情報を一部変更して使用する。

data/users.json

[
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "New York",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    }
  },
  {
    "id": 2,
    "name": "Ervin Howell",
    "username": "Antonette",
    "email": "Shanna@melissa.tv",
    "address": {
      "street": "Victor Plains",
      "suite": "Suite 879",
      "city": "Wisokyburgh",
      "zipcode": "90566-7771",
      "geo": {
        "lat": "-43.9509",
        "lng": "-34.4618"
      }
    },
    "phone": "010-692-6593 x09125",
    "website": "anastasia.net",
    "company": {
      "name": "Deckow-Crist",
      "catchPhrase": "Proactive didactic contingency",
      "bs": "synergize scalable supply-chains"
    }
  },
  {
    "id": 3,
    "name": "Clementine Bauch",
    "username": "Samantha",
    "email": "Nathan@yesenia.net",
    "address": {
      "street": "Douglas Extension",
      "suite": "Suite 847",
      "city": "New York",
      "zipcode": "59590-4157",
      "geo": {
        "lat": "-68.6102",
        "lng": "-47.0653"
      }
    },
    "phone": "1-463-123-4447",
    "website": "ramiro.info",
    "company": {
      "name": "Romaguera-Jacobson",
      "catchPhrase": "Face to face bifurcated interface",
      "bs": "e-enable strategic applications"
    }
  }
]

scripts/splitUsers.js

const fs = require('fs');
const users = require('../data/users.json');

const fn = () => {
  const NewYorker = users.filter(user => {
    if (user.address.city === 'New York') {
      return true
    }
  })
  fs.writeFile('../public/json/NewYork.json', JSON.stringify(NewYorker), err => {
    if (err) console.log(err)
    else console.log('success')
  })
}
fn()

・fs.writeFileの第一引数で格納するデータの入った出力ファイルのパスを指定する。
・第二引数は、string | Buffer | TypeArray | DataView | Object のいずれかの型になる。
・jsonを指定したいときは、JSON.stringify()で文字列(string)型にする必要がある。

このようにすると、public/jsonの配下に条件別に分割されたjsonとして、NewYork.jsonを作成することができる。

実行の際は、実行ファイルのあるディレクトリに移動し、

node 実行ファイル名

で実行する。

##参考
https://www.notion.so/Node-fs-3383f45c2cc34f26a04631254f7039cf#fdb261edcd934c95bdac72bd9e2ef70d
https://www.notion.so/Node-fs-3383f45c2cc34f26a04631254f7039cf#1ad568abc03f49ef9260d2f31c28b2ca
https://www.notion.so/Node-fs-3383f45c2cc34f26a04631254f7039cf#c8cd52db90464bf0b15d521e7aaf64c5

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?