This commit is contained in:
2022-03-14 03:36:21 -05:00
commit bdbb199d0c
7 changed files with 1036 additions and 0 deletions

33
index.js Normal file
View File

@@ -0,0 +1,33 @@
const fs = require('fs'),
[fileName, jsonPath] = process.argv.slice(2);
if (!fileName) {
console.log('Please provide a file name.');
console.log('Usage: json file.json jsonPath');
process.exit();
}
try {
const file = fs.readFileSync(fileName).toString(),
json = JSON.parse(file);
let result = json;
if (jsonPath) {
result = jsonPath.split('.').reduce((obj, key) => obj[key], json);
}
console.log(result);
if (typeof result === 'object') {
console.log('\n');
if (Array.isArray(result)) {
console.log('Array', result.length);
} else {
console.log('Keys', Object.keys(result))
}
}
} catch (err) {
console.error(err);
}