{"id":13555271,"url":"https://github.com/maxlath/jsondepth","last_synced_at":"2025-03-02T16:31:39.479Z","repository":{"id":57285693,"uuid":"57298858","full_name":"maxlath/jsondepth","owner":"maxlath","description":"a small command-line tool to walk through the depth levels of a JSON objects","archived":false,"fork":false,"pushed_at":"2021-09-14T07:34:04.000Z","size":269,"stargazers_count":11,"open_issues_count":1,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-12T07:56:01.749Z","etag":null,"topics":["cli","depth","json","parser"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maxlath.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-04-28T12:06:20.000Z","updated_at":"2024-05-04T15:50:50.000Z","dependencies_parsed_at":"2022-09-18T14:33:02.790Z","dependency_job_id":null,"html_url":"https://github.com/maxlath/jsondepth","commit_stats":null,"previous_names":[],"tags_count":31,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxlath%2Fjsondepth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxlath%2Fjsondepth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxlath%2Fjsondepth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxlath%2Fjsondepth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maxlath","download_url":"https://codeload.github.com/maxlath/jsondepth/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241537366,"owners_count":19978521,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","depth","json","parser"],"created_at":"2024-08-01T12:03:07.292Z","updated_at":"2025-03-02T16:31:39.457Z","avatar_url":"https://github.com/maxlath.png","language":"JavaScript","readme":"# jsondepth\nA small command-line tool to walk through the depth levels of a json objects\n\n`output json | jd \u003cPATH\u003e=. \u003cDEPTH\u003e=0`\n\n![screenshot](https://cloud.githubusercontent.com/assets/1596934/19417977/b2025c36-93ba-11e6-85fa-6bd274eb6be4.png)\n\n## Summary\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n\n\n- [Motivation](#motivation)\n- [Installation](#installation)\n- [How-to](#how-to)\n  - [Specify a depth level](#specify-a-depth-level)\n  - [Specify a path](#specify-a-path)\n  - [Specify a depth and a path](#specify-a-depth-and-a-path)\n  - [Access JS objects attributes](#access-js-objects-attributes)\n    - [Native attributes](#native-attributes)\n      - [Arrays `length`](#arrays-length)\n    - [Special keys](#special-keys)\n      - [`_keys`](#_keys)\n      - [`_values`](#_values)\n      - [`_map`](#_map)\n      - [`_first`](#_first)\n      - [`_last`](#_last)\n  - [Format the output as valid JSON](#format-the-output-as-valid-json)\n  - [Parse newline delimited JSON](#parse-newline-delimited-json)\n- [See also](#see-also)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n## Motivation\nWorking with super deep json objects from the terminal is a pain, unless you use a good json parser.\u003cbr\u003e\n[jq](https://github.com/stedolan/jq) is an awesome one, but doesn't handle object depths, afaik.\u003cbr\u003e\nHere the idea is to **walk through a json object as you would read a summary**: level by level.\n\n## Installation\n\n```sh\nnpm install -g jsondepth\n```\n## How-to\n\nreal world example:\n```sh\nurl='https://www.wikidata.org/w/api.php?action=wbgetentities\u0026ids=Q1\u0026format=json'\ncurl -s \"$url\" | jsondepth\n```\nlogs the object with only the first-level keys and values:\n```js\n{ entities: [Object], success: 1 }\n```\nfor the sake of convenience and lazyness, **jsondepth is aliased to jd**\n\u003cbr\u003e\n(which, purposedly make it look a bit like jq)\n```\ncurl -s \"$url\" | jd\n```\n\n### Specify a depth level\nthis is equivalent to the two previous one:\n```\ncurl -s \"$url\" | jd 0\n```\n\u003chr\u003e\n\nnow let's go one-level deeper:\n\n```sh\ncurl -s \"$url\" | jd 1\n```\noutputs:\n```js\n{ entities: { Q1: [Object] }, success: 1 }\n```\n\u003chr\u003e\n\n![we need to go deeper](http://vignette3.wikia.nocookie.net/glee/images/6/6f/We-need-to-go-deeper_inception.jpg/revision/latest)\n\n```sh\ncurl -s \"$url\" | jd 2\ncurl -s \"$url\" | jd 3\ncurl -s \"$url\" | jd 4\n# etc\n```\n\u003chr\u003e\nIf you use [paths](#specify-a-path), you may wish to disable object wrapping: this can be done by passing `-1`. The advantage is that you are sure to get back a valid json object.\n\n```sh\ncurl -s \"$url\" | jd -1\n```\n\n### Specify a path\n```sh\ncurl -s \"$url\" | jd entities.Q1.aliases.fi.1\n# or to mimick jq syntax\ncurl -s \"$url\" | jd .entities.Q1.aliases.fi.1\n# or with keys with spaces\ncurl -s \"$url\" | jd .entities.Q1.['a key with spaces']\n```\n\n### Specify a depth and a path\nif both a path and a depth are specified, **path should come first, depth second**\n```sh\ncurl -s \"$url\" | jd entities.Q1.claims.P31.0 3\n```\n\n### Access JS objects attributes\n#### Native attributes\n##### Arrays `length`\n```sh\ncurl -s \"$url\" | jd entities.Q1.aliases.en.length\n# =\u003e 5\n```\n#### Special keys\n##### `_keys`\napply `Object.keys` to the final object\n```sh\ncurl -s \"$url\" | jd entities._keys\n# =\u003e ['Q1']\n```\n##### `_values`\napply `_.values` to an array\n```sh\ncurl -s \"$url\" | jd entities._values\n# =\u003e [{ id: 'Q1', etc...}]\ncurl -s \"$url\" | jd entities._values.0.id\n# =\u003e 'Q1'\n```\n##### `_map`\nmap properties of an array\n```sh\ncurl -s \"$url\" | jd entities._values._map.id\n# =\u003e ['Q1']\n```\n##### `_first`\n```sh\necho '[\"foo\", \"bar\"]' | jd ._first\n# =\u003e foo\n```\n\n##### `_last`\n```sh\necho '[\"foo\", \"bar\"]' | jd ._first\n# =\u003e bar\n```\n\n### Format the output as valid JSON\nWrapped 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`\n```\ncurl -s \"$url\" | jd entities.Q1.aliases --json\n```\nYou can even specify the output indentation level (Between 0 and 9. Default: 2):\n```\ncurl -s \"$url\" | jd entities.Q1.aliases --json=0\ncurl -s \"$url\" | jd entities.Q1.aliases --json=4\n```\n\nNotice that it disables the depth option as it's responsible for the wrapping mechanism.\n\n### Parse newline delimited JSON\nAdd the `--line` option to parse the stdin line by line\n```\ncurl https://some.ndjson.dump | jd --line .key\n```\nThere is a tolerance for JSON lines ending by a comma, and any line that isn't starting by a `{` or a `}` is ignored.\n\n## See also\n* [jq](https://stedolan.github.io/jq/)\n* [fx](https://github.com/antonmedv/fx)\n","funding_links":[],"categories":["cli","JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxlath%2Fjsondepth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaxlath%2Fjsondepth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxlath%2Fjsondepth/lists"}