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

@ruso/flatten-json

v0.2.0

Published

FlattenJSON is a utility function for flattening JSON objects into a single-level object with keys representing the accessor path to the value

Downloads

4

Readme

FlattenJSON

FlattenJSON is a utility function for flattening JSON objects into a single-level object with keys representing the accessor path to the value.

Installation

To install FlattenJSON, you can use the npm or yarn package managers.

$ npm install @ruso/flatten-json

# or

$ yarn add @ruso/flatten-json

Usage

To use FlattenJSON, you can import the flattenJSON function from the @ruso/flatten-json package and pass a JSON object as the first argument. The function will return a flattened version of the input object, with keys representing the accessor path to the value.

import { flattenJSON } from '@ruso/flatten-json';

const json = {
	numbers: [1, 2, 3],
	person: {
		name: 'Ruslan',
		hobbies: ['programming', 'music']
	}
};

const flattenedJSON = flattenJSON(json);

console.log(flattenedJSON);

/**
Output:
{ 
  "numbers[0]": 1,
  "numbers[1]": 2,
  "numbers[2]": 3,
  "person.name": "Ruslan",
  "person.hobbies[0]":
  "programming",
  "person.hobbies[1]": "music"
}
*/

If you want to flatten a JSON array instead of a JSON object, you can use the flattenJSON function in the following way:

import { flattenJSON } from 'flatten-json';

const jsonArray = [
	{
		name: 'Ruslan',
		age: 30
	},
	{
		name: 'Sara',
		age: 28
	}
];

const flattenedJSON = flattenJSON(jsonArray);

console.log(flattenedJSON);
/*
Output:
{
  "[0].name": "Ruslan",
  "[0].age": 30,
  "[1].name": "Sara",
  "[1].age": 28
}
*/

Use cases

  • Extracting specific data from a JSON object.

  • Transforming JSON data for use in different formats or systems.

  • Simplifying the manipulation of complex JSON data.

  • Generating accessor paths for JSON data.

  • Debugging or inspecting JSON data.

    These use cases illustrate some of the potential benefits of using the flattenJSON library, such as the ability to extract specific data from a JSON object, to transform the data for use in different contexts, and to simplify the manipulation of complex JSON data. The flattenJSON function can also be useful for generating accessor paths for JSON data, and for debugging or inspecting JSON data.

Inspiration

The flattenJSON function is inspired by the way that data visualization tools like Kibana and Grafana represent JSON objects using a hierarchical structure. These tools use the keys of the JSON object as labels for the nested levels of the hierarchy, which allows users to easily access and explore the data contained in the object.

However, when working with JSON objects in a programmatic environment, it is often more convenient to access the data using a flattened representation of the object, where the keys represent the accessor path to the value. This allows users to easily access and manipulate the data contained in the JSON object, without having to navigate the hierarchical structure of the object.

The flattenJSON function provides this flattened representation of a JSON object, using the keys of the object as the accessor paths to the values. This allows users to easily access and manipulate the data contained in the JSON object, using a simple and intuitive key-value format.

Contributions

The flattenJSON function is an open-source library, and we welcome contributions from the community. If you have suggestions for improving the function, or if you have found a bug or an issue, you can open an issue on the GitHub repository for the library.

We also welcome pull requests from users who want to contribute code or other changes to the library. If you want to submit a pull request, please make sure to follow the guidelines and conventions used in the project, and to include tests for your changes. This will help us review and merge your changes more efficiently, and ensure that the library remains stable and reliable.

Thank you for your interest in contributing to the flattenJSON function, and we look forward to your feedback and contributions.

Author