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

jproc

v1.0.3

Published

Simple JSON array query tool written in javascript with nodejs

Downloads

4

Readme

Jproc

Jproc is a light-weight simple json array query tool written in JavaScript.

Installation

npm install jproc

or,

npm i jproc

Sample data.json

[
    {
        "name": "name_1",
        "age": 18,
        "details": {
            "id": "hvnakds2342349",
            "location": "London",
            "company": "xyz",
            "keywords": [
                "test1",
                "test2",
                "test3"
            ]
        }
    },
    {
        "name": "name_2",
        "age": 20,
        "details": {
            "id": "iuefibe8362873287",
            "location": "London",
            "company": "abc",
            "keywords": [
                "test1",
                "test2",
                "test3"
            ]
        }
    },
    {
        "name": "name_3",
        "age": 19,
        "details": {
            "id": "iwhiuvwi766579",
            "location": "New York",
            "company": "xyz",
            "keywords": [
                "test1",
                "test2",
                "test3"
            ]
        }
    },
    {
        "name": "name_4",
        "age": 25,
        "details": {
            "id": "wuwcuwey652362",
            "location": "Iraq",
            "company": "pqr",
            "keywords": [
                "test1",
                "test2",
                "test3"
            ]
        }
    }
]

Example usage:

import Jproc from "jproc";
import fs from "fs";

let data = JSON.parse(fs.readFileSync("./data.json"));


// Initialize Jproc object with json data.
let jObj = new Jproc(data);


//------------------------------------ Example 1 ----------------------------------------

// Optionally you can set which parameters you want in the resultant array form the original json object.
jObj.neededParams(["name", "details.id"]);

// Set query for the jproc object, if no query is set using setQuery() method, then it will return resultant array 
// with the fields specified by neededParams() method. If no parameter or query specified, then it will throw error.
jObj.setQuery("(age >= 20 || (age > 18 && details.company = 'xyz')) && (details.location = 'London' || details.location = 'New York')");

// Finally call exec_query() method to execute the query.
console.log(JSON.stringify(jObj.exec_query()));

/*
 It matches the condition and returns the fields specified in the neededParams() method called before.
 output:
 --------
[
  {
    "name": "name_2",
    "id": "iuefibe8362873287"
  },
  {
    "name": "name_3",
    "id": "iwhiuvwi766579"
  }
]
*/
// ----------------------------------- End of Example 1 -------------------------------------


// -------------------------------------- Example 2 -----------------------------------------

// use clearParams() method to clear the parameters set by the neededParams() method.
jObj.clearParams();

console.log(JSON.stringify(jObj.exec_query()));

/*
 It will match the condition and return all the fields form the json object as we have cleared the parameters set for the jproc object.
 Output:
 -------
 [
  {
    "name": "name_2",
    "age": 20,
    "details": {
      "id": "iuefibe8362873287",
      "location": "London",
      "company": "abc",
      "keywords": [
	"test1",
	"test2",
	"test3"
      ]
    }
  },
  {
    "name": "name_3",
    "age": 19,
    "details": {
      "id": "iwhiuvwi766579",
      "location": "New York",
      "company": "xyz",
      "keywords": [
	"test1",
	"test2",
	"test3"
      ]
    }
  }
]
*/
// ------------------------------- End of Example 2 ---------------------------------


// ------------------------------- Example 3 ----------------------------------------

jObj.neededParams(["name", "details.id", "details.location"]);

// Use clearQuery() method to clear existing query for the jproc object.
jObj.clearQuery();

console.log(JSON.stringify(jObj.exec_query()));

/*
 It doesn't match the condition as we have cleared the query and returns the fields form all the json elements for the array
 that are specified in the neededParams() method before.
Output:
-------
[
  {
    "name": "name_1",
    "id": "hvnakds2342349",
    "location": "London"
  },
  {
    "name": "name_2",
    "id": "iuefibe8362873287",
    "location": "London"
  },
  {
    "name": "name_3",
    "id": "iwhiuvwi766579",
    "location": "New York"
  },
  {
    "name": "name_4",
    "id": "wuwcuwey652362",
    "location": "Iraq"
  }
]
*/
// ------------------------------ End of Example 3 ---------------------------------


// ------------------------------ Example 4 ----------------------------------------

jObj.clearQuery();
jObj.clearParams();

console.log(JSON.stringify(jObj.exec_query()));

/*
This will throw error as we have cleared both parameters and query.
*/
// ------------------------------ End of Example 4 ---------------------------------