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

jsonsql

v0.2.5

Published

A SQL-like query language for JSON objects.

Downloads

320

Readme

jsonSQL

npm version

A SQL-like query language for JSON objects.

Install

npm install jsonsql

Semantics

  • All Fields *
  • Result Subset *.second_lvl.third_lvl
  • Condition where
  • AND &&
  • OR ||
  • Group ()
  • Equal =
  • Not Equal !=
  • Like ~
  • Not Like !~

Generic query

Data Source:
var dataSource = {
    '4866102f06de4f38bc30592e001cf423': {
        beat: {},
        holy: {addr: '88:63:df:a0:d7:03', name: 'CREEPY'},
        user: '',
        config: {},
        id: '4866102f06de4f38bc30592e001cf423',
        state: 2
    },
    'd07872f7d2e8447bbe874bbfd3fb0296': {
        beat: {},
        holy: {
            addr: '5c:51:4f:50:3a:88',
            name: 'CREEPY'
        },
        user: '',
        config: {},
        id: 'd07872f7d2e8447bbe874bbfd3fb0296',
        state: 1
    },
    'f2de1084a1f142258d5849428f09e39f': {
        beat: {},
        holy: {
            addr: '77:32:8a:c5:dd:63',
            name: 'REDSHIT'
        },
        user: '',
        config: {},
        id: 'f2de1084a1f142258d5849428f09e39f',
        state: 0,
        date: '2012-05'
    },
    'cff9580b2efa4a2ba84784c1ac80eb09': {
        beat: {},
        holy: {
            addr: '7f:e8:ee:32:cd:15',
            name: 'CRAP'
        },
        user: '',
        config: {},
        id: 'cff9580b2efa4a2ba84784c1ac80eb09',
        state: 1
    }
};  

Query with condition:

var Query = require('jsonSQL');
var res = Query(dataSource, '* where holy.name=CRAP || (holy.name=CREEPY && (state=1 || state=2))');
console.log('======RESULT 1\n',res);
RESULT OUTPUT:
======RESULT 1
 [ { beat: {},
    holy: { addr: '88:63:df:a0:d7:03', name: 'CREEPY' },
    user: '',
    config: {},
    id: '4866102f06de4f38bc30592e001cf423',
    state: 2 },
  { beat: {},
    holy: { addr: '5c:51:4f:50:3a:88', name: 'CREEPY' },
    user: '',
    config: {},
    id: 'd07872f7d2e8447bbe874bbfd3fb0296',
    state: 1 },
  { beat: {},
    holy: { addr: '7f:e8:ee:32:cd:15', name: 'CRAP' },
    user: '',
    config: {},
    id: 'cff9580b2efa4a2ba84784c1ac80eb09',
    state: 1 } ]

Multi-Fields Query:

var res = Query(dataSource, '*.id, *.holy.addr where holy.name=CRAP');
console.log('======RESULT 2\n',res);
RESULT OUTPUT:
======RESULT 2
 [ { id: 'cff9580b2efa4a2ba84784c1ac80eb09',
    'holy.addr': '7f:e8:ee:32:cd:15' } ]

Comparsion

Data Source:
var dataSource = {
  '4866102f06de4f38bc30592e001cf423': {
    name: 'Tom',
    age: 33,
    birth: new Date(2016, 1, 1)
  },
  'd07872f7d2e8447bbe874bbfd3fb0296': {
    name: 'Jack',
    age: 18,
    birth: new Date(2015, 1, 1)
  },
  'd935869b80f542a9bf3f6a59d4f635f1': {
    name: 'Peter',
    age: 26,
    birth: new Date(2014, 1, 1)
  },
};

Number comparison:

var Query = require('jsonSQL');
var res = Query(dataSource, '* where age<=30');
console.log('======RESULT 1\n',res);
RESULT OUTPUT:
======RESULT 1
 [ { name: 'Jack',
    age: 18,
    birth: Sun Feb 01 2015 00:00:00 GMT+0800 (CST) },
  { name: 'Peter',
    age: 26,
    birth: Sat Feb 01 2014 00:00:00 GMT+0800 (CST) } ]
Date comparison:
var Query = require('jsonSQL');
var res = Query(dataSource, '* where birth>' + new Date(2015,1,1).valueOf());
console.log('======RESULT 2\n',res);
RESULT OUTPUT:
======RESULT 2
 [ { name: 'Tom',
    age: 33,
    birth: Mon Feb 01 2016 00:00:00 GMT+0800 (CST) } ]

Date string comparison:

Data Source:
var dataSource = [
  {
    "Version": "120",
    "CreateDate": "2013-05-25T06:50:25.584Z",
    "Desc": "abcdef"
  },
  {
    "Version": "121",
    "CreateDate": "2014-05-23T06:50:25.584Z",
    "Desc": "dfdsf"
  },
  {
    "Version": "122",
    "CreateDate": "2015-05-26T06:50:25.584Z",
    "Desc": "qewert"
  },
  {
    "Version": "123",
    "CreateDate": "2016-05-24T06:50:25.584Z",
    "Desc": "xfsstd"
  }
];
query:
var Query = require('jsonSQL');
var res = Query(dataSource, '* where CreateDate>' + new Date(2014,1,1).valueOf());
console.log('======RESULT 1\n',res);
RESULT OUTPUT:
======RESULT 1
 [ { Version: '121',
    CreateDate: '2014-05-23T06:50:25.584Z',
    Desc: 'dfdsf' },
  { Version: '122',
    CreateDate: '2015-05-26T06:50:25.584Z',
    Desc: 'qewert' },
  { Version: '123',
    CreateDate: '2016-05-24T06:50:25.584Z',
    Desc: 'xfsstd' } ]