New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

json-selector-lang

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-selector-lang

A tiny language for selecting JSON values

latest
Source
npmnpm
Version
1.1.0
Version published
Weekly downloads
50
400%
Maintainers
1
Weekly downloads
 
Created
Source

JSON Selector Language

A tiny language for selecting JSON values

JSON Selector Language, or JSL, can be used to dynamically select JSON values. It is meant for use in client-side environments like the browser where bandwidth and performance constraints exist.

It was inspired by projects like jq and JMESPath, but is tiny by design— it only has two expressions.

Usage

As an example, if your data looks like the following:

const json = {
  data: {
    Data: {
      items: [1, 3, 5, 7],
    },
  },
};

Select the Data object:

import { jsl } from 'json-selector-lang';

try {
  const sel = jsl.compile('.data.Data');
  const val = jsl.evaluate(json, sel);

  // val = { items: [1, 3, 5, 7] }
} catch (e) {
  // catch the parser errors
}

Select the items array:

import { jsl } from 'json-selector-lang';

try {
  const sel = jsl.compile('.data.Data.items');
  const val = jsl.evaluate(json, sel);

  // val = [1, 3, 5, 7]
} catch (e) {
  // catch the parser errors
}

Select the third entry in the items array:

import { jsl } from 'json-selector-lang';

try {
  const sel = jsl.compile('.data.Data.items[2]');
  const val = jsl.evaluate(json, sel);

  // val = 5
} catch (e) {
  // catch the parser errors
}

Constraints

The main constraint in v1 is that it can only evaluate JSON where the outermost type is object.

// can evaluate
{ data: [0,1,2] }

// cannot evaluate
[ {data: 0} ]

// cannot evaluate
42

// cannot evaluate
"data"

// cannot evaluate
null

Tips

💡 Compile your selectors once and reuse as much as possible.

Keywords

json

FAQs

Package last updated on 20 Jul 2022

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