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

json-spread

v0.3.2

Published

simple javascript utility to spread nested json or javascript objects

Downloads

48

Readme

Json-Spread

npm license github-issues npm-downloads

Description

A simple javascript library that flattens a json structured object and then creates duplicate objects off of each nested array elements.

Great for converting nested, multi-leveled json to single level json that can be used to create csv,tsv,excel or other row column structured data.

Installation

Node

npm install json-spread

Browser

include the jsonSpread.min.js file from the dist folder

Usage

var jsonSpread = require('json-spread');
var output = jsonSpread({
  "a": [
    {
      "index": 1
    },
    {
      "index": 2
    },
    {
      "index": 3
    }
  ]
})
/*
output = [
  {
    "a.index": 1
  },
  {
    "a.index": 2
  },
  {
    "a.index": 3
  }
]
*/

Examples

nested array

//input
{
  "a": [
    1,
    2,
    3
  ],
  "b": {
    "a": [
      1,
      2,
      3
    ]
  }
}
//output
[
  {
    "a":1
  },
  {
    "a":2
  },
  {
    "a":3
  },
  {
    "b.a":1
  },
  {
    "b.a":2
  },
  {
    "b.a":3
  }
]

nested arrays within nested objects

//input
{
  "a": {
    "b": {
      "c": {
        "d": {
          "e" : {
            "array": [
              1,
              2,
              3
            ]
          }
        }
      }
    }
  }
}
//output
[
  {
    "a.b.c.d.e.array": 1
  },
  {
    "a.b.c.d.e.array": 2
  },
  {
    "a.b.c.d.e.array": 3
  }
]

real life example

//input
[
  {
    "user_id" : 1,
    "email": "[email protected]",
    "hobbies": [
      {
        "type": "sport",
        "name": "soccer",
        "dates": [
          "May 3rd",
          "May 4th",
          "May 5th"
        ]
      },
      {
        "type": "sport",
        "name": "basketball",
        "dates": [
          "June 3rd",
          "July 4th"
        ]
      }
    ]
  },
  {
    "user_id" : 2,
    "email": "[email protected]"
  },
  {
    "user_id" : 3,
    "email": "[email protected]",
    "hobbies": []
  }
]
//output
[{
	"user_id": 1,
	"email": "[email protected]",
	"hobbies.type": "sport",
	"hobbies.name": "soccer",
	"hobbies.dates": "May 3rd"
}, {
	"user_id": 1,
	"email": "[email protected]",
	"hobbies.type": "sport",
	"hobbies.name": "soccer",
	"hobbies.dates": "May 4th"
}, {
	"user_id": 1,
	"email": "[email protected]",
	"hobbies.type": "sport",
	"hobbies.name": "soccer",
	"hobbies.dates": "May 5th"
}, {
	"user_id": 1,
	"email": "[email protected]",
	"hobbies.type": "sport",
	"hobbies.name": "basketball",
	"hobbies.dates": "June 3rd"
}, {
	"user_id": 1,
	"email": "[email protected]",
	"hobbies.type": "sport",
	"hobbies.name": "basketball",
	"hobbies.dates": "July 4th"
}, {
	"user_id": 2,
	"email": "[email protected]"
}, {
	"user_id": 3,
	"email": "[email protected]",
	"hobbies": null
}]

Options

Fields

delimiter

specify the delimiting value for nested objects.

var data = { "a": { "b" : "foo"} };
var options = {
  delimiter : "*" //default is '.'
}
var output = jsonSpread(data,options);
//output
{
  "a*b" : "foo"
}
removeEmptyArray

removes empty arrays

var data = { "a": "value_a" , "b": []};
var options = {
  removeEmptyArray: true //default is false
}
var output = jsonSpread(data,options);
//output
{
  "a" : "value_a"
}
emptyValue

you can define the value for empty arrays in options.
this is ignored if removeEmptyArray is true

var data = { "a": [] };
var options = {
  emptyValue: "EMPTY" //default is null
}
var output = jsonSpread(data,options);
//output
{
  "a" : "EMPTY"
}

Contributing

installation

Fork it, then do an npm install. everything should be in there

building

after writing in src folder, do:

npm run dist

to see if it builds

tests

I use mocha and chai to test.

npm test

write tests in /test folder.

Dependencies

This library currently depends on flat

License

MIT License