Skip to content

Instantly share code, notes, and snippets.

@ryu1
Last active July 8, 2019 03:35
Show Gist options
  • Select an option

  • Save ryu1/67e5d7fd586ef732ebe32190f4660ac3 to your computer and use it in GitHub Desktop.

Select an option

Save ryu1/67e5d7fd586ef732ebe32190f4660ac3 to your computer and use it in GitHub Desktop.
ファイルを一行ずつ読み込みJSON形式かチェックする。主に、DynamoDBへData Pipelineを使用して投入するファイルのJSONが壊れてないかチェックする目的で使用。
'use strict';
// ex.)
// node validater.js ./import-file.txt
//
var env = process.env.NODE_ENV || 'development'
const fs = require("fs")
const readline = require("readline")
class Counter {
constructor() {
this.currentValue = 0
}
count() {
this.currentValue++
}
get value() {
return this.currentValue
}
set value(val) {
this.currentValue = val
}
}
if (require.main === module) {
main({ argv: process.argv })
}
function main(options) {
const argv = options.argv
const path = argv[2]
// console.info("argv", argv)
const stream = fs.createReadStream(path, "utf8")
const reader = readline.createInterface({ input: stream })
const counter = new Counter()
reader.on("line", (data) => {
counter.count()
try {
//console.log(data)
JSON.parse(data)
} catch (err) {
console.error("line:" + counter.value, err, data)
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment