New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

gson-query

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gson-query

json-pointer utilities for querying data

  • 1.0.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
681
increased by3.65%
Maintainers
1
Weekly downloads
 
Created
Source

Json Query

Install

npm i gson-query

and use with

const query = require("gson-query")

At first, json-query acts like a normal json-pointer where its match is passed to the given callback function:

	const query = require("gson-query");
	const data = {
		"parent": {
			"child": {"id": "child-1"}
		}
	};
	query.run(data, "#/parent/child/id", (value, key, object, jsonPointer) => {
		// value = "child-1"
		// key = "id"
		// object = {"id": "child-1"}
		// jsonPointer = "#/parent/child/id"
	});
	// or get the result in an array
	var match = query.get(data, "#/parent/child/id");
	// [ ["child-1", "id", {"id":"child-1"}, "#/parent/child/id"] ]

But query also supports glob-patterns with *:

	const query = require("gson-query");
	const data = {
		"parent": {
			"child": {"id": "child-1"}
		},
		"neighbour": {
			"child": {"id": "child-2"}
		}
	};
	query.run(data, "#/*/child/id", function (value, key, object, jsonPointer) {
		// will be called with value: "child-1" and "child-2"
	});
	// or get the result in an array
	var match = query.get(data, "#/parent/child/id");
	// [ ["child-1", ...], ["child-2", ...] ]

and glob-patterns with **:

	var query = require("gson-query");
	var data = {
		"parent": {
			"child": {"id": "child-1"}
		},
		"neighbour": {
			"child": {"id": "child-2"}
		}
	};
	query.run(data, "#/**/id", function (value, key, object, jsonPointer) {
		// will be called with value: "parent" "child-1" and "child-2"
	});

or simply call query.run(data, "#/**", callback) to run callback on each object,array and value.

To filter the matched objects, an object-query string may be appended on each single step:

	var query = require("gson-query");
	var data = {
		"parent": {
			"valid": true,
			"child": {"id": "child-1"}
		},
		"neighbour": {
			"valid": false,
			"child": {"id": "child-2"}
		},
		"dungeons": {
			"child": {"id": "child-3"}
		}
	};
	query.run(data, "#/**?valid:true&&ignore:undefined/child", function (value, key, object, jsonPointer) {
		// will be called with value: {"id": "child-1"} only
	});
	// same result with
	query.run(data, "#/**?valid:!false/child", function (value, key, object, jsonPointer) { // ...

or match all objects that have a defined property valid like query.run(data, "#/**?valid", callback).

regular expression must be wrapped with {.*}:

	var query = require("gson-query");
	var data = {
		"albert": {valid: true},
		"alfred": {valid: false},
		"alfons": {valid: true}
	};
	query.run(data, "#/{al[^b]}?valid:true", function (value, key, object, jsonPointer) {
		// will be executed with value: alfons
	});

queryGet

If you only require values or pointers, use queryGet to receive an Array as result:

	var queryGet = require("gson-query").get;

	// default: queryGet.VALUES
	var arrayOfValues = queryGet(data, "#/**/id", queryGet.VALUE);
	// ["#/..", "#/..", ...]
	var arrayOfJsonPointers = queryGet(data, "#/**/id", queryGet.POINTER);
	// [arguments, arguments], where arguments = 0:value 1:object 2:key 3:jsonPointer
	var arrayOfAllFourArguments = queryGet(data, "#/**/id", queryGet.ALL);

queryDelete

Multiple items on objects or in arrays may also be delete with query.delete:

	var queryDelete = require("gson-query").delete;

	queryDelete(data, "#/**/*/data");

Examples

  • query.run(data, "#/**/*", callback); will iterate over each value of the data object
  • query.run(data, "#/**?valid:true", callback); will select all objects having its property "valid" set to true

for further examples refer to the unit tests

FAQs

Package last updated on 21 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc