Created
February 24, 2025 20:29
-
-
Save yene/6cf3688ce3ff8c1eb9dd0eedb491d9cf to your computer and use it in GitHub Desktop.
script to sort json by given json file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fs = require('fs') | |
let config = { | |
undefinedKeys: 'last', // 'alphabetical' or 'last' | |
updateInPlace: true, | |
} | |
function main() { | |
let command = process.argv[2] | |
let filePath = process.argv[3] | |
if (!filePath) { | |
console.error('Please provide a file path as an argument.') | |
process.exit(1) | |
} | |
if (command === 'learn') { | |
fs.readFile(filePath, 'utf8', (err, data) => { | |
if (err) { | |
console.error('Error reading the file:', err) | |
process.exit(1) | |
} | |
try { | |
let jsonData = JSON.parse(data) | |
let orderJSON = extractJSONOrder(jsonData) | |
console.log(JSON.stringify(orderJSON, null, 2)) | |
} catch (parseErr) { | |
console.error('Error parsing JSON:', parseErr) | |
process.exit(1) | |
} | |
}) | |
} else if (command === 'apply') { | |
let orderFilePath = process.argv[4] | |
if (!orderFilePath) { | |
console.error('Please provide a file path as an argument.') | |
process.exit(1) | |
} | |
let inJSON = fs.readFileSync(filePath, 'utf8') | |
let inData = JSON.parse(inJSON) | |
let orderJSON = fs.readFileSync(orderFilePath, 'utf8') | |
let orderData = JSON.parse(orderJSON) | |
let orderedJSON = orderJSON2(inData, orderData) | |
let res = JSON.stringify(orderedJSON, null, 2) | |
if (config.updateInPlace) { | |
fs.writeFileSync(filePath, res, 'utf8') | |
} else { | |
console.log(res) | |
} | |
} | |
} | |
function orderJSON2(inData, orderData) { | |
if (Array.isArray(inData)) { | |
for (let i=0; i<inData.length; i++) { | |
inData[i] = orderJSON2(inData[i], orderData[0]) | |
} | |
return inData //inData.map(orderJSON2) | |
} else if (typeof inData === 'object') { | |
let clonedObj = {} | |
let inKeys = Object.keys(inData) | |
let orderKeys = Object.keys(orderData) | |
inKeys.sort((a, b) => { | |
let aIndex = orderKeys.indexOf(a) | |
let bIndex = orderKeys.indexOf(b) | |
if (aIndex === -1 || bIndex === -1) { | |
if (config.undefinedKeys === 'last') { | |
return +1 | |
} else { | |
return a.localeCompare(b) | |
} | |
} | |
return orderKeys.indexOf(a) - orderKeys.indexOf(b) | |
}) | |
for (let key of inKeys) { | |
clonedObj[key] = orderJSON2(inData[key], orderData[key]) | |
} | |
return clonedObj | |
} else { | |
return inData | |
} | |
} | |
function extractJSONOrder(jsonData) { | |
if (Array.isArray(jsonData)) { | |
return jsonData.slice(0, 1).map(extractJSONOrder) | |
} else if (typeof jsonData === 'object') { | |
let clonedObj = {} | |
for (let key in jsonData) { | |
clonedObj[key] = extractJSONOrder(jsonData[key]) | |
} | |
return clonedObj | |
} else { | |
return typeof jsonData | |
} | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment