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

json-assign-value

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-assign-value

assign value to JSON based on query

latest
Source
npmnpm
Version
0.0.4
Version published
Maintainers
1
Created
Source

json-assign-value

Assign values to JSON directly, supports direct assignments, callback and promises

browser directly!

Install via npm,

$ npm install json-assign-value

API

var jsonAssignValue = require('json-assign-value')

jsonAssignValue(query, asigningValue ,options)

Specify a query, what to assign and what to query. Returns a new objects with values assigned.


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

jsonAssignValue('people[country=NZ].name', 'John',  {
  data: data
}) //=> {people[{name: 'John', country: 'NZ'} ... etc

jsonAssignValue('people[country=NZ].name', (value) => `${value} Regan`,  {
  data: data, cb: true
}) //=> {people[{name: 'Matt Regan', country: 'NZ'} ... etc

jsonAssignValue('people[country=NZ].name', new Promise(resolve) => resolve("John"),  {
  data: data, asyncify: true
}) //=> {people[{name: 'John', country: 'NZ'} ... etc

jsonAssignValue('people[country=NZ].name', (value) => new Promise(resolve) => resolve(`${value} Regan`),  {
  data: data, cb: true, asyncify: true
}) //=> {people[{name: 'Matt Regan', country: 'NZ'} ... etc

Options:

  • data The main object to assign values.
  • allowRegexp (optional): Enable the ~ operator. Before enabling regexp match to anyone, consider the user defined regular expression security concerns.
  • cb Boolean, this allows users to set value as a callback function with the matched variable available in the callback.
  • asyncify Boolean, this allows users to pass promises as the values to be set

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 pluck

people.name => return all the names of people

Array filter

By default only the first matching item will be returned:

people[name=Matt]

But if you add an asterisk (*), all matching items will be returned:

people[*country=NZ]

You can use comparative operators:

people[*rating>=3]

Or use boolean logic:

people[* rating >= 3 & starred = true]

If options.enableRegexp is enabled, you can use the ~ operator to match RegExp:

people[*name~/^R/i]

You can also negate any of the above examples by adding a ! before the = or ~:

people[*country!=NZ]

License

MIT

Keywords

JSON

FAQs

Package last updated on 18 Jun 2021

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