GithubHelp home page GithubHelp logo

nvdnkpr / json-query Goto Github PK

View Code? Open in Web Editor NEW

This project forked from auditassistant/json-query

0.0 2.0 0.0 140 KB

Retrieves values from JSON objects for data binding

Home Page: https://npmjs.org/package/json-query

License: MIT License

json-query's Introduction

JSON Query

Retrieves values from JSON objects for data binding. Offers params, nested queries, deep queries, custom reduce/filter functions and simple boolean logic.

Used internally by JSON Context for data binding.

Install

$ npm install json-query

jsonQuery(query, options)

Specify a query and what to query - returns an object that describes the result of the query.

var jsonQuery = require('json-query')

var context = {
  people: [
    {name: 'Matt', country: 'NZ'},
    {name: 'Pete', country: 'AU'}
  ]
}

jsonQuery('people[country=NZ].name', {
  rootContext: context
}) //=> {value: 'Matt', parents: [...], key: 0} ... etc

options:

  • rootContext: The main JS object to query.
  • context (optional): The current object we're interested in. Is accessed in query by starting with .
  • parent (optional): An additional context for looking further up the tree. Is accessed by ..
  • filters: Specify an object containing filter functions. Accessed by ':filterName'
  • force (optional): Specify an object to be returned from the query if the query fails - it will be saved into the place the query expected the object to be.

Queries

Queries are strings that describe an object or value to pluck out, or manipulate from the context object. The syntax is a little bit CSS, a little bit JS, but pretty powerful.

Accessing properties (dot notation)

person.name

Array accessors

people[0]

Array filter

people[country=NZ]

Or syntax

person.greetingName|person.name

Deep queries

Search through multiple levels of Objects/Arrays

var context = {
  grouped_people: {
    'friends': [
      {name: 'Steve', country: 'NZ'},
      {name: 'Bob', country: 'US'}
    ],
    'enemies': [
      {name: 'Evil Steve', country: 'AU'}
    ]
  }
}

jsonQuery('grouped_people[][country=NZ]', {rootContext: context})

Inner queries

var context = {
  page: {
    id: 'page_1',
    title: 'Test'
  },
  comments_lookup: {
    'page_1': [
      {id: 'comment_1', parent_id: 'page_1', content: "I am a comment"}
    ]
  }
}

// get the comments that match page's id
jsonQuery('comments_lookup[{page.id}]', {rootContext: context})

Filter functions

Allows to to hack the query system to do just about anything.

Some nicely contrived examples:

var filters = {
  greetingName: function(input){
    if (input.knownAs){
      return input.known_as
    } else {
      return input.name
    }
  }
  },
  and: function(input, params){
    return input && params.args[0]
  },
  text: function(input, params){
    return params.args[0]
  },
  then: function(input, params){
    if (input){
      return params.args[0]
    } else {
      return params.args[1]
    }
  }
}

var context = {
  is_fullscreen: true,
  is_playing: false,
  user: {
    name: "Matthew McKegg",
    known_as: "Matt"
  }
}

jsonQuery('user:greetingName', {
  rootContext: context, filters: filters
}).value //=> "Matt"

jsonQuery(['is_fullscreen:and({is_playing}):then(?, ?)', "Playing big!", "Not so much"], {
  rootContext: context, filters: filters
}).value //=> "Not so much"

jsonQuery(':text(This displays text cos we made it so)', {
  filters: filters
}).value //=> "This displays text cos we made it so"

Context

Specifying context ('rootContext', 'context', and 'parent' options) is good for databinding and working on a specific object and still keeping the big picture available.

var data = {
  styles: {
    bold: 'font-weight:strong',
    red: 'color: red'
  },
  paragraphs: [
    {content: "I am a red paragraph", style: 'red'},
    {content: "I am a bold paragraph", style: 'bold'},
  ],
}

var pageHtml = ''
data.paragraphs.forEach(function(paragraph){
  var style = jsonQuery('styles[{.style}]', {rootContext: data, context: paragraph}).value
  var content = jsonQuery('.content', rootContext: data, context: paragraph) // pretty pointless :)
  pageHtml += "<p style='" + style "'>" + content + "</p>"
})

Query Params

Params can be specified by passing in an array with the first param the query (with ? params) and subsequent params.

jsonQuery(['people[country=?]', 'NZ'])

License

MIT

json-query's People

Contributors

mmckegg avatar

Watchers

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