json-partial
v1.0.0
Published
Parses a json file looking for partials. If found the partial is read and applied to the json file. On exit saves the json with the provided output name.
Downloads
10
Readme
json-partial
This module will allow to use json "partials" and expand them into another json file. Sometimes you have one or more json snippets and you may want to use them to build a bigger json file. One example is when you are using json to generate templates with handlebars or mustache.
Installation
npm install json-partial
Usage
Suppose you have a JSON snippet that is repeated almost without change in several places of a large JSON file.
{
"charRemaining": "50",
"counterText": "characters remaining",
"fieldName": "customerName",
"fieldSize": "large",
"labelPosition": "",
"labelText": "Customer Name",
"maxLength": "50",
"placeHolder": "Enter customer name",
"value": ""
}
You want to use almost the same values in two places:
{
"userName": {
"charRemaining": "50",
"counterText": "characters remaining",
"fieldName": "userName",
"fieldSize": "large",
"labelPosition": "",
"labelText": "User Name",
"maxLength": "50",
"placeHolder": "enter user name",
"value": ""
},
"password": {
"charRemaining": "50",
"counterText": "characters remaining",
"fieldName": "password",
"fieldSize": "large",
"labelPosition": "",
"labelText": "Password",
"maxLength": "50",
"placeHolder": "enter password",
"value": "",
"inputType": "password"
}
}
With json-partial you can rewrite the file like this:
{
"jsonx": [
{
"file": "bar.json",
"field": "userName",
"data": {
"fieldName": "userName",
"labelText": "User Name",
"placeHolder": "enter user name"
}
},
{
"file": "bar.json",
"field": "password",
"data": {
"fieldName": "password",
"labelText": "Password",
"placeHolder": "enter password",
"inputType": "password"
}
}
]
}
You will have several advantages:
- The file is cleaner and only the data that changes is required
- You will need to modify only one instance of the snippet in case of a change or bugs