npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

json-assign-value

v0.0.4

Published

assign value to JSON based on query

Downloads

28

Readme

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