2.0.5 • Published 4 years ago

jsondepth v2.0.5

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

jsondepth

A small command-line tool to walk through the depth levels of a json objects

output json | jd <PATH>=. <DEPTH>=0

screenshot

Summary

Motivation

Working with super deep json objects from the terminal is a pain, unless you use a good json parser. jq is an awesome one, but doesn't handle object depths, afaik. Here the idea is to walk through a json object as you would read a summary: level by level.

Installation

npm install -g jsondepth

How-to

real world example:

url='https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q1&format=json'
curl -s "$url" | jsondepth

logs the object with only the first-level keys and values:

{ entities: [Object], success: 1 }

for the sake of convenience and lazyness, jsondepth is aliased to jd (which, purposedly make it look a bit like jq)

curl -s "$url" | jd

Specify a depth level

this is equivalent to the two previous one:

curl -s "$url" | jd 0

now let's go one-level deeper:

curl -s "$url" | jd 1

outputs:

{ entities: { Q1: [Object] }, success: 1 }

we need to go deeper

curl -s "$url" | jd 2
curl -s "$url" | jd 3
curl -s "$url" | jd 4
# etc
curl -s "$url" | jd -1

Specify a path

curl -s "$url" | jd entities.Q1.aliases.fi.1
# or to mimick jq syntax
curl -s "$url" | jd .entities.Q1.aliases.fi.1
# or with keys with spaces
curl -s "$url" | jd .entities.Q1.['a key with spaces']

Specify a depth and a path

if both a path and a depth are specified, path should come first, depth second

curl -s "$url" | jd entities.Q1.claims.P31.0 3

Access JS objects attributes

Native attributes

Arrays length
curl -s "$url" | jd entities.Q1.aliases.en.length
# => 5

Special keys

_keys

apply Object.keys to the final object

curl -s "$url" | jd entities._keys
# => ['Q1']
_values

apply _.values to an array

curl -s "$url" | jd entities._values
# => [{ id: 'Q1', etc...}]
curl -s "$url" | jd entities._values.0.id
# => 'Q1'
_map

map properties of an array

curl -s "$url" | jd entities._values._map.id
# => ['Q1']
_first
echo '["foo", "bar"]' | jd ._first
# => foo
_last
echo '["foo", "bar"]' | jd ._first
# => bar

Format the output as valid JSON

Wrapped results like { entities: { Q1: [Object] }, success: 1 } are more readible but aren't valid JSON. And sometimes, for whatever reason, you want a valid JSON output; there in a option for that: --json|-j

curl -s "$url" | jd entities.Q1.aliases --json

You can even specify the output indentation level (Between 0 and 9. Default: 2):

curl -s "$url" | jd entities.Q1.aliases --json=0
curl -s "$url" | jd entities.Q1.aliases --json=4

Notice that it disables the depth option as it's responsible for the wrapping mechanism.

Parse newline delimited JSON

Add the --line option to parse the stdin line by line

curl https://some.ndjson.dump | jd --line .key

There is a tolerance for JSON lines ending by a comma, and any line that isn't starting by a { or a } is ignored.

See also

2.0.5

4 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.9.3

6 years ago

1.9.2

7 years ago

1.9.1

8 years ago

1.9.0

8 years ago

1.8.0

8 years ago

1.7.0

9 years ago

1.6.3

9 years ago

1.6.2

9 years ago

1.6.1

9 years ago

1.6.0

9 years ago

1.5.1

9 years ago

1.5.0

9 years ago

1.4.1

9 years ago

1.4.0

9 years ago

1.3.6

9 years ago

1.3.5

9 years ago

1.3.4

9 years ago

1.3.3

9 years ago

1.3.2

10 years ago

1.3.1

10 years ago

1.3.0

10 years ago

1.2.2

10 years ago

1.2.1

10 years ago

1.2.0

10 years ago

1.1.0

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago