npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

read-complex-json

v1.0.4

Published

A npm library for reading and fetching values from a complex json object. You just need to pass the key you want and it will give the value/values wherever it is present in the json object.

Downloads

12

Readme

read-complex-json

Introducing a compact and lightweight library, with an unpacked size of just 1.8kB, that offers an efficient and streamlined approach to handling JSON data. This library empowers developers to effortlessly retrieve the values associated with any key, regardless of its location within the JSON structure. With its minimal footprint, it provides a powerful tool for working with JSON data in a highly optimized manner.

How to Use -

getValues() function

const jsonReader = require('read-complex-json')
//data: your json data(either json string or json object, both will work)
//key: for which you want to find value/values of
const values = jsonReader.getValues(data, key)
//Do something with values

Examples -

const jsonData = {
  "name": "Ram",
  "age": 30,
  "address": {
    "street": "Cannaught Place",
    "city": "New Delhi",
    "state": "New Delhi",
    "pin": "123456"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": 999
    },
    {
      "type": "work",
      "number": 888
    }
  ]
};

console.log(jsonReader.getValues(jsonData, 'name'))
//Output: ["Ram"]

console.log(jsonReader.getValues(jsonData, 'number'))
//Output: [999, 888]

console.log(jsonReader.getValues(jsonData, 'phoneNumbers'))
/*Output: [[
    {
      "type": "home",
      "number": 999
    },
    {
      "type": "work",
      "number": 888
    }
  ]]*/

console.log(jsonReader.getValues(jsonData, 'keyNotExists'))
//Output: []

What if json object is not valid

const jsonData = 'Just a string'; //invalid data
console.log(jsonReader.getValues(jsonData, 'someKey'))
//Output: Not a valid json!! Please check again

getValuesFromFile() function

const jsonReader = require('read-complex-json')
//filePath: path of the file where json is stored
//key: for which you want to find value/values of
const values = jsonReader.getValuesFromFile(filePath, key)
//Do something with values

Examples -

const filePath = 'C:/ayush.garg/Documents/read-complex-json/data.json';

//file Data
/*{
  "name": "Ram",
  "age": 30,
  "address": {
    "street": "Cannaught Place",
    "city": "New Delhi",
    "state": "New Delhi",
    "pin": "123456"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": 999
    },
    {
      "type": "work",
      "number": 888
    }
  ]
}*/


console.log(jsonReader.getValuesFromFile(filePath, 'name'))
//Output: ["Ram"]

console.log(jsonReader.getValuesFromFile(filePath, 'number'))
//Output: [999, 888]

console.log(jsonReader.getValuesFromFile(filePath, 'phoneNumbers'))
/*Output: [[
    {
      "type": "home",
      "number": 999
    },
    {
      "type": "work",
      "number": 888
    }
  ]]*/
 
console.log(jsonReader.getValuesFromFile(filePath, 'keyNotExists'))
//Output: []

What if json object is not valid

const filePath = 'C:/ayush.garg/Documents/read-complex-json/data.txt';   //Not a json file or invalid json
console.log(jsonReader.getValuesFromFile(filePath, 'someKey'))
//Output: Not a valid json file!! Please check again

For any inquiries, questions, or clarifications regarding this library, we encourage you to reach out to us via email at [email protected]. We are here to provide prompt assistance and address any doubts you may have about the library's functionality, implementation, or any other related concerns. Your feedback is valuable to us, and we are committed to ensuring a seamless experience with our library. Please don't hesitate to contact us; we look forward to assisting you.