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-paring-knife

v1.0.1

Published

A command line tool for paring keys out of JSON files and writing them to new files

Downloads

2

Readme

JSON Paring Knife (jpk)

Simple CLI tool for paring entries out of JSON files and migrating them to new files.

The intent of this tool is to move identical keys that exist across multiple JSON files to a new folder, matching each of the original filenames. It was built for the use case of i18n strings being stored in per-language JSON files, e.g. in folder Component/translations/ you have en.json, fr.json, ko.json... and want to delete certain entries in these files and copy them to new files in another folder NewComponent/translations/.

It assumes that all JSON files in a given folder have an identical shape; at the moment it does not (gracefully) handle other cases.

Installation

The package is available from NPM under the name json-paring-knife.

npm install -g json-paring-knife

Alternatively, you can also clone this repo and run this from the repo root:

npm install -g

Check for a successful installation with json-paring-knife -v; it should print the package's version number.

Usage

The script can be run with json-paring-knife or the shorthand jpk. All script arguments are available in verbose and shorthand equivalents (e.g. --source == -s).

list

List all available keys from the JSON file passed in as --source.

jpk list --source ./Component/translations/en.json

If a folder is provided, it uses the first JSON file in the folder. Again, this is because it's expecting all JSON files in the folder to have the same shape.

jpk list -s ./Component/translations/

pare

Pare selected --keys from --source, save to --destination folder.

If --source is a single file, it only pares that file.

jpk pare --source ./Component/translations/en.json --destination ./NewComponent/translations/ --keys keyOne,keyTwo.nestedKey.deepNestedKey,keyThree

If --source is a folder, it pares all JSON files in that folder.

jpk pare -s ./Component/translations/ -d ./NewComponent/translations/ -k keyOne,keyTwo.nestedKey.deepNestedKey,keyThree

Example

Starting with these files:

// Component/translations/en.json
{
  "1": "one",
  "2": {
    "a": "two aye",
    "b": "two bee"
  },
  "3": {
    "a": {
      "i": "three aye one",
      "ii": "three aye two"
    },
    "b": "three bee"
  },
  "4": "four",
  "5": {
    "a": "five aye"
  }
}

// Component/translations/fr.json
{
  "1": "un",
  "2": {
    "a": "deux ah",
    "b": "deux bay"
  },
  "3": {
    "a": {
      "i": "trois ah un",
      "ii": "trois ah deux"
    },
    "b": "trois bay"
  },
  "4": "quatre",
  "5": {
    "a": "cinq ah"
  }
}

Get a list of the keys you can select:

jpk list -s ./Component/translations/en.json

# Result
All keys in the file ./Component/translations/en.json:
  1
  2.a
  2.b
  3.a.i
  3.a.ii
  3.b
  4
  5.a

Pare the keys 3.a and 4 from source folder ./Component/translations/, move to destination folder ./NewComponent/translations/:

jpk pare -s ./Component/translations/ -d ./NewComponent/translations/ -k 3.a,4

Resulting original files:

// ./Component/translations/en.json
{
  "1": "one",
  "2": {
    "a": "two aye",
    "b": "two bee"
  },
  "3": {
    "b": "three bee"
  },
  "5": {
    "a": "five aye"
  }
}

// ./Component/translations/fr.json
{
  "1": "un",
  "2": {
    "a": "deux ah",
    "b": "deux bay"
  },
  "3": {
    "b": "trois bay"
  },
  "5": {
    "a": "cinq ah"
  }
}

Resulting new files:

// ./NewComponent/translations/en.json
{
  "3": {
    "a": {
      "i": "three aye one",
      "ii": "three aye two"
    }
  },
  "4": "four"
}

// ./NewComponent/translations/fr.json
{
  "3": {
    "a": {
      "i": "trois ah un",
      "ii": "trois ah deux"
    }
  },
  "4": "quatre"
}