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

json-q

v0.1.11

Published

Retrieves values from JSON objects (and JavaScript objects) by css-selector-like query (includes attribute filters and array flattening).

Downloads

24

Readme

json-q

Retrieves values from JSON objects (and JavaScript objects) by css-selector-like query (includes attribute filters and array flattening).

Coverage Status Build Status

I am not clever enough to use XPath over objects (via jsonpath, jsonata, JSONPath, ObjectPath or DefiantJs), while I like CSS selectors. JSONSelect looks abandoned, json-query looks overcomplicated; so I created more simple query language (inspired by CSS attribute selectors).

Example

const {get} = require('json-q');

const data = {
  a:{
    b:[
      {name:'xxx',c:{d:1}},
      {name:'yyy',c:{d:2}}
    ]
  }
};

get(data, "a b[.c.d=1] name"); //=> ['xxx']

API

get(object, selector, opt)

Returns array of all fields of object from any level of nesting that satisfies selector (with expansions via opt).

About selectors:

  • "a"   means: get value of all fields named "a" from all nested level of given object
  • ".a" means: get value of field named "a" from first level of given object (i.e. object["a"])
  • "a b" means: get all values of all fields "b", that are nested of field "a", that can be at any level of given object
  • ".a.b" means: get field "b", that is direct descendant of field "a" from first level of given object (i.e. object.a.b)

About filters:

  • you can add filter of any depth at any level like this: "a.b[x.y=23] c"
  • combination of filters "[.x=23][.y=3]" means "items heaving field x=23 AND field y=3"
  • you can use [attr] [attr=value] [attr~=value] [attr|=value] [attr^=value] [attr$=value] [attr*=value] [attr=value] - just like CSS attribute filters do

About pseudos:

  • do you remember CSS pseudo-classes? All that :focus, :active, :hover etc.? Pretty useless for objects, even :empty and :first-child, but it is a good concept to add user-defined (parameterless) functions.
  • you can add it anywhere: "a b:empty.c"
  • look at :empty and see the section about expansions

Another thing - I consider array as multiple values of field, so

  1. arrays of arrays become flat, i.e. {a:[[1], [2,3]]} becomes {a:[1, 2, 3]}}

  2. you can not address array items by index, i.e.


var data = {
  a:{
    b:{
      c:[1,2]
    }
  }
};

get(data, ".a.b.c"); //=> [1,2]
get(data, ".a.b.c.0"); //=> []


var data = {
  a:{
    b:[
      {c:1},
      {c:2}
    ]
  }
}

get(data, ".a.b.c"); //=> [1,2] also

Escaping Special Characters

There are no strings now. So if you have special symbols at field names then you should escape it - I mean dot, colon, square brackets and space symbols (i.e. " .:[]").


var data = {
  "a:c":{
      x:[1,2]
  }
};

get(data, "a:c"); //=> Error "Pseudo 'c' not found."
get(data, "a\\:c"); //=> {x:[1, 2]}

Expansions (i.e. opt param at get)

You can add your own filter or pseudo (or re-define existing one). The difference between them is that filter can only filter (obviously) while pseudo can do anything with intermediate result - i.e. delete, add, change (at any depth) objects at result array.

For instance, new filter for [a!=some value]

var d = [{a:{name:1}}, {a:{name:2}}]
var p = "a[name!=1]";

get( d, p, {
  operator : {
    "!=" : function(complexFieldValue, value){
      return true_if_one_is_true(complexFieldValue, value, (a,b)=>{return a!=b;});
    },
  }
}); // => [{name:2}]

And pseudo for add "abc" string to all fields names at any level (dont ask me what for)

var d = [{a:{b:1}}, {a:{c:2}}]
var p = "a:abc.cabc";

get( d, p, {
  pseudo : {
    "abc" : function(arrValue){
      return arrValue.map(value => {
        deep_iterate(value, (_obj) => {
          for(var i in _obj) {
            if (typeof _obj[i] !== 'object') {
              _obj[i+'abc'] = _obj[i];
              delete _obj[i];
            }
          }
        });
        return value;
      })
    },
  }
}); // => [2]

Browser

Please use index.min.js at browser (IE9+ and other modern browsers)

License

MIT