0.1.1 • Published 4 years ago

@paulsmith/jsonconverter v0.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
4 years ago

JSON Converter

This is yet another tool for converting JSON data composed in one format into JSON with some other schema. It supports:

  • Querying the source document with JMESPath expressions
  • JSON-serializable templates
  • Custom transformation logic (processors)
  • Chaining processors
  • Async processors
  • References

Getting Started

$ npm install @paulsmith/jsonconverter

Usage

Basic Usage

import { Converter } from '@paulsmith/jsonconverter'

const myTemplate = {
  mappings: [
    {
      path: 'milleniumFalcon.pilot', // Where the data will go in the target
      query: 'pilot', // Where to find the data in the source
      processors: ['trim', 'upper'] // Apply transformations
    }
  ]
}
const sourceData = { pilot: ' Han Solo  ' }
const converter = new Converter(myTemplate)

converter.render(sourceData).then(converted => console.log(converted.result))
// { milleniumFalcon: { pilot: 'HAN SOLO' } }

Add an arbitrary value to target JSON

const myTemplate = {
  mappings: [
    { path: 'isScoundrel', value: true },
    { path: 'pal', value: 'Lando' },
    { path: 'bestPal', value: 'Chewie' }
  ]
}
const sourceData = { name: 'Han Solo' }
const converter = new Converter(myTemplate)

converter.render(sourceData).then(converted => console.log({ ...sourceData, ...converted.result }))
// {
//  name: 'Han Solo',
//  isScoundrel: true,
//  pal: 'Lando',
//  bestPal: 'Chewie'
// }

Apply a custom function as a processor

const myTemplate = {
  mappings: [
    {
      path: 'hanSolo.kesselRun.parsecs',
      query: 'kesselRunParsecs',
      processors: [p => --p]
    }
  ]
}
const sourceData = { kesselRunParsecs: 13 }
const converter = new Converter(myTemplate)

converter.render(sourceData).then(converted => console.log(converted.result))
// { hanSolo: { kesselRun: { parsecs: 12 } } }

Compose processors by chaining them together

// Compose outcomes by chaining processors
const myTemplate = {
  mappings: [
    { path: 'name', query: 'name', processors: ['trim', 'upper'] },
    {
      path: 'bestPal',
      processors: [() => 'Chewie', 'upper']
    }
  ]
}
const sourceData = { name: '     Han Solo   ' }
const converter = new Converter(myTemplate)

converter.render(sourceData).then(converted => console.log({ ...sourceData, ...converted.result }))
// { name: 'HAN SOLO', bestPal: 'CHEWIE' }

Built-in Processors

ProcessorInputProducesArgumentsDescription
upperStringStringChanges the input to all uppercase
lowerStringStringChanges the input to all lowercase
trimStringStringRemoves leading and trailing spaces
joinArrayStringjoiner (default ', ')Joins the Array elements into a single string, joined by the joiner
mapArrayArraymapping functionApplies a mapping function to the array desc
queryAnyAnyA JMESPath queryApplies the JMESPath query to the provided value
slugifyStringStringSlugifies the string
titleCaseStringStringReturns the provide string In Title Case
toJsonAnyStringApplies JSON.stringify
sortArrayArraySorting functionSorts the provided array
sliceStringStringstart, endReturns part of a string
dateFormatStringStringApplies toISOString to a date string
fetchString (a URL)Anyparams object which will be passed to axiosTakes in a URL from the source, makes a request to the URL, and puts the response body into the target JSON

Acknowledgements

This library would have been less fun to write, and significantly less fun to test without the Paul Hallett's awesome SWAPI Star Wars API. May The Force be with you.

Built with

:heart:

0.1.1

4 years ago

0.1.0

4 years ago

0.0.1

4 years ago

0.0.1-3

4 years ago

0.0.1-2

4 years ago

0.0.1-1

4 years ago

0.0.1-0

4 years ago