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 🙏

© 2024 – Pkg Stats / Ryan Hefner

jscout

v3.24.6

Published

Jscout aims to make everyday operations with JSON easier

Downloads

255

Readme

jscout

Jscout is a utility designed to make everyday JSON work easier. It allows you to traverse JSON structures, update values effortlessly, check JSON validity, and even correct invalid JSON formats. With its straightforward API, it simplifies searching and manipulating deeply nested JSON objects or arrays for specific data.

Contributing

If you have ideas for making JSON operations even easier, feel free to submit a pull request on GitHub. Contributions are always welcome!

Installation

To install jscout, use npm or yarn:

npm install jscout

or

yarn add jscout

Usage

Jscout provides a simple method to search through complex JSON objects to find a value by key. Below are some examples that demonstrate how you can use the utility.

Importing jscout

You can import jscout using ES Modules:

import jscout from "jscout";

Make sure your package.json have

"type": "module"

Or if you are using CommonJS:

const jscout = require("jscout").default;

Example Usage

Here is an example of how you can use jscout to find a value in a JSON object:

import jscout from "jscout";

const sampleJson = {
  name: "Alice",
  address: {
    city: "Wonderland",
    zip: "12345",
    details: {
      state: "Fantasy",
      country: "Magicland",
    },
  },
  hobbies: [
    { name: "Reading", level: "Advanced" },
    { name: "Exploring", location: "Wonderland" },
  ],
};

// Example 1: Finding the first occurrence of 'city'
const city = jscout.find(sampleJson, "city");
console.log("City:", city); // Output: 'Wonderland'

// Example 2: Finding the first occurrence of 'name'
const hobbyName = jscout.find(sampleJson, "name");
console.log("First name occurrence:", hobbyName); // Output: 'Alice'

// Example 3: Updating the 'city' field
jscout.update(sampleJson, "city", "New Wonderland");
console.log("Updated city:", sampleJson.address.city); // Output: 'New Wonderland'

// Example 4: Validating a JSON string
const validJsonString = '{"name": "Alice", "age": 25}';
const invalidJsonString = '{name: "Alice", age: 25}';

console.log(
  "Is valid JSON (validJsonString):",
  jscout.isValid(validJsonString)
); // Output: true
console.log(
  "Is valid JSON (invalidJsonString):",
  jscout.isValid(invalidJsonString)
); // Output: false

// Example 5: Fixing a malformed JSON string
const brokenJsonString = `
"name": ""Alice"", 
"age": 25, 
"address": {
    "street": ""123 Main St"",
    "city": "Wonderland",
    "zipcode": "12345"
,
"contact": {
    "email": ""[email protected]"",
    "phone": ""555-1234""
}
`;

try {
  const fixedJson = jscout.fix(brokenJsonString);
  console.log("Fixed JSON:", fixedJson);
  // Output: Corrected JSON object
} catch (error) {
  console.error("Could not fix JSON:", error.message);
}

Handling Complex Structures

jscout is designed to recursively traverse both objects and arrays to find the first occurrence of the specified key. It works with deeply nested structures and supports searching through mixed arrays of objects and other data types.

License

Jscout is licensed under the MIT License. See the LICENSE file for more details.

Contact

If you have any questions or suggestions, please open an issue on GitHub.