npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

132

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

  1. Installation
  2. Usage
  3. Configuration
  4. Examples
  5. Custom Functions
  6. License

Installation

You can install the JsonDataProcessor library via npm:

npm install json-data-processor

Usage

To use the JsonDataProcessor, you'll need to:

  1. Import the necessary dependencies
  2. Create an instance of JsonDataProcessor by passing in a configuration object.
  3. 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:

  1. Directly passed as a function.
  2. 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.