GithubHelp home page GithubHelp logo

doytsujin / jsonsql Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rockdragon/jsonsql

0.0 1.0 0.0 29 KB

A SQL-like query language for JSON objects.

License: MIT License

JavaScript 100.00%

jsonsql's Introduction

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' } ]

jsonsql's People

Contributors

rockdragon 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.