Coder Social home page Coder Social logo

giorgiob-xilinx / jsonpath-object-transform Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dvdln/jsonpath-object-transform

0.0 0.0 0.0 2.54 MB

Transform an object literal using JSONPath.

License: MIT License

JavaScript 88.63% HTML 11.37%

jsonpath-object-transform's Introduction

jsonpath-object-transform

Transform an object literal using JSONPath.

npm

Pulls data from an object literal using JSONPath and generate a new objects based on a template. Each of the template's properties can pull a single property from the source data or an array of all results found by its JSONPath. When pulling an array of data you can also supply a subtemplate to transform each item in the array.

JSONPath is like XPath for JavaScript objects. To learn the syntax, read the documentation for the JSONPath package on npm and the original article by Stefan Goessner.

Usage

var transform = require('jsonpath-object-transform');

var template = {
  foo: ['$.some.crazy', {
    bar: '$.example'
  }]
};

var data = {
  some: {
    crazy: [
      {
        example: 'A'
      },
      {
        example: 'B'
      }
    ]
  }
};

var result = transform(data, template);

Result:

{
  foo: [
    {
      bar: 'A'
    },
    {
      bar: 'B'
    }
  ]
}

Method

jsonPathObjectTransform(data, template);

Where data and template are both a plain Object. Returns the transformed Object.

Template Objects

Your template will be an object literal that outlines what the resulting object should look like. Each property will contain a JSONPath String or Array depending on how many properties from the source data you want to assign to the generated object.

Pulling a Single Property

{ destination: '$.path.to.source' }

Use a String on your template property to assign a single object returned from your JSONPath. If your path returns multiple results then only the first is used.

Example

var template = {
  foo: '$.example'
};

var data = {
  example: 'bar'
};

Result:

{
  foo: 'bar'
}

Pulling an Array of Properties

{ destination: ['$.path.to.sources'] }

Use an Array containing a single String to assign all results returned from your JSONPath.

Example

var template = {
  foo: ['$..example']
};

var data = {
  a: {
    example: 'bar'
  },
  b: {
    example: 'baz'
  }
};

Result:

{
  foo: ['bar', 'baz']
}

Transform Items Returned in Array

{ destination: ['$.path.to.sources', { item: '$.item.path' }] }

Use an Array with a String and an Object to assign all results returned from your JSONPath and transform each of the objects with a subtemplate.

Example

var template = {
  foo: [$..example, {
    bar: '$.demo'
  }]
};

var data = {
  a: {
    example: {
      demo: 'baz'
    }
  },
  b: {
    example: {
      demo: 'qux'
    }
  }
};

Result:

{
  foo: [
    { bar: 'baz' },
    { bar: 'qux' }
  ]
}

jsonpath-object-transform's People

Contributors

drpoggi avatar dvdln avatar rueckstiess avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.