UNPKG

json-morph

Version:
127 lines (91 loc) โ€ข 3.3 kB
# ๐ŸฆŽ json-morph [![NPM version](https://img.shields.io/npm/v/json-morph.svg?style=flat)](https://www.npmjs.com/package/json-morph) [![NPM downloads](https://img.shields.io/npm/dm/json-morph.svg?style=flat)](https://npmjs.org/package/json-morph) **Transform JSON like a boss.** Filter it. Reshape it. Clean it. Remix it. All from your terminal. > _"Why mutate your JSON by hand when you can morph it like a shapeshifter?"_ --- ## โœจ What is `json-morph`? `json-morph` is a command-line tool and API that helps you **transform JSON data** using simple, expressive flags. - ๐Ÿง  Filter using logical expressions (`age > 18 && status == 'active'`) - ๐Ÿ” Map keys to new paths (`user.name` โ†’ `fullName`) - โž• Add static fields (`role:admin`) - โŒ Remove sensitive fields (`password`, `secret`) - ๐Ÿ’… Pretty-print or export to a new file Perfect for quick one-liners, data munging, CLI pipelines, config transforms, and replacing brittle `jq` scripts for common tasks. --- ## ๐Ÿš€ Installation ```bash npm install -g json-morph ``` ## ๐Ÿ› ๏ธ Usage ```bash json-morph input.json [options] ``` ### Options | Flag | Description | | --- | --- | | --map | Remap keys: "from.path:to.path" | | --filter | Filter expression: "age > 18 && active == true" | | --add | Add static fields: "key:value" | | --remove | Remove fields: "password,token" | | --preserve | Preserve all original fields (used with --map) | | --output, -o | Output to a file | | --pretty | Pretty-print output | | --help | Show help | | --version | Show version | ### ๐Ÿ” Examples ## ๐Ÿงผ Filter and Clean ```bash npx json-morph users.json \ --filter "age > 21 && active == true" \ --remove "password,internalNote" \ --output active-users.json ``` ## ๐Ÿ”„ Remap Keys ```bash npx json-morph input.json \ --map "user.name:fullName" \ --map "user.id:userId" \ --preserve ``` ## โž• Add Fields ```bash npx json-morph input.json \ --add "status:active" \ --add "score:100" ``` ## ๐Ÿงช Combine It All ```bash npx json-morph data.json \ --filter "score >= 80" \ --map "user.email:contact.email" \ --add "passed:true" \ --remove "debugLog" \ --pretty ``` ## ๐Ÿงฌ Programmatic Usage You can also use json-morph as a library: ```javascript import { transformJson } from 'json-morph'; const data = [{ name: 'Alice', age: 25 }]; const flags = { filter: ['age > 18'], map: ['name:fullName'], add: ['status:active'], }; const output = transformJson(data, flags); console.log(output); ``` ## โšก Why Use json-morph? * โœ… Lightweight, dependency-free * โœ… Safer and easier than writing ad hoc scripts * โœ… Works with deeply nested JSON * โœ… Expressive filter logic without learning jq * โœ… Scriptable, composable, and fun โœจ ## ๐Ÿ‘ค About the Author Built by [cinfinit](https://github.com/cinfinit), a part-time JSON whisperer, full-time logic artisan, and lifelong member of the "why-is-this-API-like-this" club. Loves: * Clean abstractions * Messy data * Terminal one-liners that look like sorcery When not mutating JSON structures, probably found arguing with a linter, renaming variables obsessively, or building another side project that absolutely no one asked for โ€” but everyone secretly needed. โ€œIf it involves curly braces and chaos, Iโ€™m probably interested.โ€