Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // jsonchecker.js
- // run as "node jsonchecker.js"
- const fs = require('fs')
- const testFolder = '.'
- const jsonFiles = fs.readdirSync(testFolder).filter((f) => f.endsWith('.json'))
- function countStrings(json) {
- return Object.keys(json).reduce((acc, currKey) => {
- const currVal = json[currKey]
- if (typeof currVal === 'string') {
- return acc + 1
- } else {
- return acc + countStrings(currVal)
- }
- }, 0)
- }
- jsonFiles.forEach((f) => {
- let rawdata = fs.readFileSync(f)
- let jsonContent = JSON.parse(rawdata)
- const strings = countStrings(jsonContent)
- console.log(`file: ${f.toString().padStart(40)}, strings: ${strings}`)
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement