normalize-json-by-json
v1.0.0
Published
Normalize JSON values and structures using a JSON specification.
Maintainers
Readme
normalize-json-by-json
Normalize JSON values and structures (arrays, numbers, strings, dates) using a JSON specification.
Installation
npm install normalize-json-by-jsonWhy normalize?
When parsing XML (like Tally), a list with one item is parsed as an { object }, while multiple items are parsed as an [ array ].
normalize-json-by-json guarantees your JSON is always uniform and typed.
Usage
import normalize from "normalize-json-by-json";
const vouchers = [
{
voucherNumber: "1",
masterId: "174169",
inventoryEntries: [
{
stockItemName: "ROPE",
amount: "100",
// Single batch parsed as an object from Tally:
batchAllocations: {
godownName: "Main Location",
batchName: "Tuf-Rs.170"
}
}
]
}
];
const spec = {
voucherNumber: "number",
masterId: "number",
inventoryEntries: [
{
amount: "number",
batchAllocations: "array" // Guarantees array structure
}
]
};
const result = normalize(vouchers, spec);
console.log(result);Output
[
{
"voucherNumber": 1,
"masterId": 174169,
"inventoryEntries": [
{
"stockItemName": "ROPE",
"amount": 100,
"batchAllocations": [
{
"godownName": "Main Location",
"batchName": "Tuf-Rs.170"
}
]
}
]
}
]Supported Types
"array"— wraps a single object into[ object ], preserves existing arrays"number"— converts numeric strings to numbers"string"— converts values to string"boolean"— converts"true"/"false"/1/0to boolean"date"— converts valid date strings toDateobjects
Validation
The specification can be validated before normalization:
import { validate } from "normalize-json-by-json";
const results = validate(spec);
const isValid = results.every(item => item.isValid);Pipeline Role
Used as Step 3 in tally-to-json:
Tally XML ➔ tally-clean-response ➔ select-json-by-json ➔ rename-json-keys ➔ normalize-json-by-jsonLicense
MIT © KeshavSoft
