Code Monkey home page Code Monkey logo

json-nav's Introduction

json-nav

json-nav is a lightweight and easy-to-use npm package that simplifies the process of extracting specific data points from complex JSON objects. With this package, you can avoid writing custom code to extract data from nested JSON structures and instead rely on a simple query string to retrieve the desired information.

Installation

npm install json-nav

Usage

import the query function and pass in a JSON object and a query string in dot notation:

const { query } = require('json-nav');

const data = {
  name: 'John',
  age: 30,
  address: {
    street: '123 Main St',
    city: 'Anytown',
    state: 'CA',
    zip: '12345'
  },
  phoneNumbers: [
    { type: 'home', number: '555-1234' },
    { type: 'work', number: '555-5678' }
  ]
};

const result = query(data, 'address.city');
console.log(result); // ['Anytown']

In this example, the query function is used to extract the value of the address.city property from the data object. The result is an array containing the value 'Anytown.'

If the query matches multiple properties, the query function will return an array containing all matching values:

const data = {
  name: 'John',
  age: 30,
  address: {
    street: '123 Main St',
    city: 'Anytown',
    state: 'CA',
    zip: '12345'
  },
  phoneNumbers: [
    { type: 'home', number: '555-1234' },
    { type: 'work', number: '555-5678' }
  ]
};

const result = query(data, 'phoneNumbers.number');
console.log(result); // ['555-1234', '555-5678']

If the query does not match any properties, the query function will return an empty array:

const data = {
  name: 'John',
  age: 30,
  address: {
    street: '123 Main St',
    city: 'Anytown',
    state: 'CA',
    zip: '12345'
  },
  phoneNumbers: [
    { type: 'home', number: '555-1234' },
    { type: 'work', number: '555-5678' }
  ]
};

const result = query(data, 'foo');
console.log(result); // []

Contributing

If you find a bug or have a feature request, please open an issue on the GitHub repository. Pull requests are also welcome!

json-nav's People

Contributors

deankamali avatar

Watchers

 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.