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

jsonutilities

v0.0.3

Published

JSONUtil module is useful to perform operations on JSON.

Downloads

3

Readme

JSONUtil

This utilities are useful to perform operations of JSON objects. The installation and usage is shown below.

Installation

npm install -g jsonutilities

Basic Object Initialization

var JSONUtil = require("jsonutilities");

JSONUtil Functions

All JSON Util function examples are shown based on following JSON Object. Let assume variable 'testJson' have a following value.
{
  "results": [
    {
      "address_components": [
        {
          "long_name": "1016",
          "short_name": "1016",
          "types": [
            "street_number"
          ]
        }
      ],
      "formatted_address": "1016 N Plum Ave, Inman, KS 67546, USA",
      "place_id": "EiUxMDE2IE4gUGx1bSBBdmUsIElubWFuLCBLUyA2NzU0NiwgVVNB",
      "types": [
        "street_address"
      ]
    },
    {
      "address_components": [
        {
          "long_name": "1016",
          "short_name": "1016",
          "types": [
            "street_number"
          ]
        }
      ],
      "formatted_address": "1016 N Plum St, Hutchinson, KS 67501, USA",
      "place_id": "EikxMDE2IE4gUGx1bSBTdCwgSHV0Y2hpbnNvbiwgS1MgNjc1MDEsIFVTQQ",
      "types": [
        "street_address"
      ]
    },
    {
      "address_components": [
        {
          "long_name": "1016",
          "short_name": "1016",
          "types": [
            "street_number"
          ]
        }
      ],
      "formatted_address": "1016 N Plum St, Springfield, OH 45504, USA",
      "place_id": "EioxMDE2IE4gUGx1bSBTdCwgU3ByaW5nZmllbGQsIE9IIDQ1NTA0LCBVU0E",
      "types": [
        "street_address"
      ]
    },
    {
      "address_components": [
        {
          "long_name": "1016",
          "short_name": "1016",
          "types": [
            "street_number"
          ]
        }
      ],
      "formatted_address": "1016 N Plum Point Rd, Himrod, NY 14842, USA",
      "place_id": "EisxMDE2IE4gUGx1bSBQb2ludCBSZCwgSGltcm9kLCBOWSAxNDg0MiwgVVNB",
      "types": [
        "street_address"
      ]
    }
  ],
  "status": "OK"
}
1. json_getValues_of_key

This function is useful to get all values of key. This function will go through the complete json object and finds all values of given key.

var result = JSONUtil.json_getValues_of_key(testJson,"formatted_address");
result = [
 "1016 N Plum Ave, Inman, KS 67546, USA",
 "1016 N Plum St, Hutchinson, KS 67501, USA",
 "1016 N Plum St, Springfield, OH 45504, USA",
 "1016 N Plum Point Rd, Himrod, NY 14842, USA"]
2. json_getKeys

This function is useful to get all values of key. This function will go through the complete json object and finds all values of given key.

var result = JSONUtil.json_getKeys(testJson,true|false);
true - It will go through the complete object including nested objects
false - It doesn't go through nested object.

 var result = JSONUtil.json_getKeys(testJson,false);
 result = ["results","status"]
 
 var result = JSONUtil.json_getKeys(testJson,true);
 result=[
 "long_name"
"short_name"
"types"
"address_components"
"formatted_address"
"place_id"
"results"
"status"];
3. json_array_length

This function is useful to get the length of an array.

var result = JSONUtil.json_array_length(testJson.results);
result = 4
4. json_extract_path_text

This function gets the value of given path.

var result = JSONUtil.json_extract_path_text(testJson, ["results","0" ,"address_components", "0", "long_name"]);
result = 1016
5. json_find_keyvalue_pair

This function finds whether the given target object is available in input or not.

var result = JSONUtil.json_find_keyvalue_pair(testJson, {"long_name": "1016", "short_name": "1016"});
result = true
6. json_concat_objects

This function concatenates json objects.

JSONUtil.json_concat_objects(jsonObject1, jsonObject2);
7. json_remove_objects_from_array_using_key

This function remove all objects having the key/value pair in json array..

JSONUtil.json_remove_objects_from_array_using_key(testJson.results, {"formatted_address": "1016 N Plum Point Rd, Himrod, NY 14842, USA"});
8. json_sort_array_using_key

This function sorts array in asc/dec using given key.

JSONUtil.json_sort_array_using_key(jsonArray, key, reverse, keyType);
reverse - false for Ascending order.
          true for Decending order.
keyType - 0 for key type is string
          1 for key type is integer
          2 for key type is float/double

ex: 
var homes = [{
            "id": "3",
            "city": "Dallas",
            "price": "162500"
        }, {
            "id": "4",
            "city": "Bevery Hills",
            "price": "319250"
        }, {
            "id": "5",
            "city": "New York",
            "price": "962500"
        }];

var sort_dec_by_id = JSONUtil.json_sort_array_using_key(homes,'id',false,1);
var sort_asc_by_city = JSONUtil.json_sort_array_using_key(sort_dec_by_id,'city',true,0);
9. json_array_reverse

This function reverse the objects in json array.

JSONUtil.json_array_reverse(jsonArray);