Giter Site home page Giter Site logo

jsonwalk's Introduction

jsonwalk npm

This tool allows you to walk a JSON object recursively given a jq-like query array, and run function transforms on the fields contained in this schema.

Example

const jsonwalk = require('jsonwalk')

const myJson = {
    "Product Name": "Bowler Hat",
      "ProductID": 858383,
      "SKU": "0406654608",
      "Description": {
              "Colour": "Purple",
              "Width": 300,
              "Height": 200,
              "Depth": 210,
              "Weight": 0.75
             },
      "Price": 34.45,
      "Quantity": 2,
    "tags": ['clothing', 'hats']

}

const query = ['Description.Width', 'Description.Height']

function makeBigger(key, value) {
    console.log(value)
    return value*2 // double the size
}

(async () => {
  jsonwalk.walk(myJson, query, makeBigger)
})()
/* should return: 
 * 300
 * 200
 */

Like in jq, you can specify array elements with $:

const query = ['Description.tags.$']

function makeBigger(key, value) {
	console.log(value)
	return value.toUpperCase()
}
(async () => {
  jsonwalk.walk(myJson, query, makeBigger)
})()
/* should return
 * clothing
 * hats
 */

You can reverse the functionality (run the callback on all fields not in the query) by adding another boolean param at the end:

jsonwalk.walk(myJson, query, makeBigger, true) // true is reversed

jsonwalk's People

Contributors

silur avatar

Watchers

 avatar  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.