Giter Club home page Giter Club logo

eval-json-path's Introduction

eval-json-path

Evaluate/Query the value of json object at given path

JavaScript Style Guide

Commonly js object variable can be accessed by property name or index. This module allow to access by path/steps given by a string

Usage

const evalJsonPath = require('eval-json-path')

const value = evalJsonPath(obj, 'prop.subprop[prett-prop][3].lastprop')

const sameValue = obj.prop.subprop['prett-prop'][3].lastprop

The evaluation is done by each step, which is null-safe, any not-existing step will result undefined or fallback

API

evalJsonPath(obj, path, fallback)

  • obj: The object should has sub property
  • path: A string represent the path/steps
  • fallback: A optional fallback for any not-existing step

More Examples

const obj = {
  step1: {
    step2: {
      'pretty.nice-step': [
        {},
        {step3: 4}
      ]
    }
  }
}

evalJsonPath(null, 'step1'); // undefined

evalJsonPath(undefined, 'step1'); // undefined

evalJsonPath(3, 'step1'); // undefined

evalJsonPath(false, 'step1'); // undefined

evalJsonPath(obj, 'step1'); // same as obj.step1

evalJsonPath(obj, 'step1.step2'); // same as obj.step1.step2

evalJsonPath(obj, 'step1.step2[pretty.nice-step]'); // obj.step1.step2['pretty.nice-step'])

evalJsonPath(obj, 'step1.step2[pretty.nice-step][1].step3'); // obj.step1.step2['pretty.nice-step'][1].step3)

evalJsonPath(obj, 'step1.xxxxx[pretty.nice-step][1].step3'); // any non-exist step result undefined

eval-json-path's People

Contributors

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