Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-extract-cli

Extract a nested JSON value using a dot-path

Install

npm install --global json-extract-cli

Usage

$ json-extract --help

  Usage
    $ json-extract <path> [options]

  Options
    --file <path>       Read from file instead of stdin
    --json              Pretty-print output as JSON
    --fallback <value>  Return fallback value if path not found
    --quiet             Suppress errors, exit 0 on failure

  Examples
    $ echo '{"user":{"email":"a@b.com"}}' | json-extract "user.email"
    a@b.com

    $ json-extract "user.name" --file data.json
    John

    $ json-extract "missing.path" --fallback "default"
    default

Examples

Extract from piped input

$ echo '{"user":{"name":"John","email":"john@example.com"}}' | json-extract "user.email"
john@example.com

Extract from file

$ json-extract "user.name" --file data.json
John

Extract nested arrays

$ echo '{"users":[{"name":"Alice"},{"name":"Bob"}]}' | json-extract "users"
[{"name":"Alice"},{"name":"Bob"}]

Pretty-print output

$ echo '{"users":[{"name":"Alice"}]}' | json-extract "users" --json
[
  {
    "name": "Alice"
  }
]

Use fallback value

$ echo '{"user":{"name":"John"}}' | json-extract "user.age" --fallback "unknown"
unknown

Quiet mode (suppress errors)

$ echo '{"user":{"name":"John"}}' | json-extract "user.age" --quiet
$ echo $?
0

API

The tool accepts JSON input from stdin or a file and extracts values using dot-notation paths.

Supported types

  • Strings
  • Numbers
  • Booleans
  • null
  • Arrays (returns full array as JSON)
  • Objects (returns full object as JSON)

Path notation

Use dot-notation to access nested properties:

  • user.name
  • data.items.0.id
  • config.settings.display.theme

Related

  • jq - More powerful JSON processor
  • fx - Interactive JSON viewer
  • json - JSON manipulation tool

License

MIT

About

Extract a nested JSON value using a dot-path

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages