snippets/old_snippets/jq-command-line-json-processor.en.md

28 lines
1.2 KiB
Markdown

---
title: "jq json processor"
date: 2020-01-30T09:10:56+01:00
draft: false
snippet_types: ["JSON", "curl", "jq"]
---
One of my oldest snippets is how to [Pretty print JSON](/snippets/pretty-print-json)
in the shell. This method works great for simple things where you just need to get an idea
of the JSON structure, it has the bonus of using python which you probably already have
installed. The problem is when you want to do more complex tasks it is quite limited in terms of
parsing. Thats where [jq](https://stedolan.github.io/jq) comes in
```shell
$ curl https://review-noticket-xxxxxxxx.eu/xxxxx/static/loadable-stats.json | jq '.entrypoints .sharedHeader .assets' | rg --invert-match map`
```
Simply piping to [jq](https://stedolan.github.io/jq) pretty prints the JSON
but by passing a query string, ex **".entrypoints .sharedHeader .assets"**,
you dig into the JSON and easily get what you need. This is easily
combinable with other shell utilities like in the example above which gets a
list of asset URLs than uses ripgrep invert-match to clean out the source map
URLs from the list. This is now my perfered way of working with JSON in the shell.
source:
- [brew formulae](https://formulae.brew.sh/formula/jq)
- [jq docs](https://formulae.brew.sh/formula/jq)