json-data-processor
v0.4.1
Published
`JsonDataProcessor` is a flexible and extensible library designed to process and transform JSON data by applying a series of configured steps. The library supports various transformations including JSONPath, VTL (Velocity Template Language), JSONata, URL
Downloads
1
Readme
JsonDataProcessor Library README
JsonDataProcessor
is a flexible and extensible library designed to process and transform JSON data by applying a series of configured steps. The library supports various transformations including JSONPath, VTL (Velocity Template Language), JSONata, URL building, and more.
Table of Contents
Installation
You can install the JsonDataProcessor
library via npm:
npm install json-data-processor
Usage
To use the JsonDataProcessor
, you'll need to:
- Import the necessary dependencies
- Create an instance of
JsonDataProcessor
by passing in a configuration object. - Call the
processData
function on the instance with the input data you want to process.
const JsonDataProcessor = require('json-data-processor');
const config = {
// Your configuration here...
};
const processor = new JsonDataProcessor(config);
const inputData = {
// Your input JSON data...
};
const result = await processor.processData(inputData);
Configuration
The configuration object passed to JsonDataProcessor
determines how the input data will be processed. This configuration primarily consists of an array of steps, each representing a specific type of transformation or action.
Steps Configuration
Each step in the configuration should specify:
- type: Type of the step, e.g., 'jsonpath', 'vtl', 'custom', 'jsonata', 'url', 'axios'.
- name: (Optional) Name of the step. If omitted, defaults to
outputStep{i}
. - input: Determines the input for this step. Can be 'original' (original input), 'previous' (output of the last step), or the name of a specific previous step.
- outputKey: The key under which the result of this step will be stored in the global state.
- output: (Optional) If set to 'global', the global state will be reset.
There are specific parameters required for each type of step, like query
for JSONPath, template
for VTL, etc.
Logging Configuration
The library uses the pino
logger, and its configuration can be passed via the logLevel
key. Supported log levels are 'trace', 'debug', 'info', etc.
Examples
1. Applying a JSONPath Transformation:
const config = {
logLevel: 'info',
steps: [
{
type: 'jsonpath',
input: 'original',
query: '$.items[0]',
outputKey: 'firstItem'
}
]
};
// Assuming input data is:
// {
// "items": ["apple", "banana", "cherry"]
// }
// The result will be:
// {
// "firstItem": "apple"
// }
2. Building a URL:
const config = {
logLevel: 'info',
steps: [
{
type: 'url',
baseURL: 'example.com',
path: ['products', { jsonata: '$.productId' }],
outputKey: 'productURL'
}
]
};
// Assuming input data is:
// {
// "productId": "123"
// }
// The result will be:
// {
// "productURL": "http://example.com/products/123"
// }
Custom Functions
The library also supports custom functions, which can be either:
- Directly passed as a function.
- Referred to by name, in which case the function should exist in the
custom-functions
module.
const config = {
logLevel: 'info',
steps: [
{
type: 'custom',
function: 'myCustomFunction', // this refers to a function in the custom-functions module
outputKey: 'customOutput'
}
]
};
License
This library is licensed under the MIT License. See LICENSE
file for details.