> For the complete documentation index, see [llms.txt](https://gchandra.gitbook.io/big-data-and-tools-with-nosql/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gchandra.gitbook.io/big-data-and-tools-with-nosql/tools/jq.md).

# JQ

jq is a lightweight and flexible command-line JSON processor.\
\
[**https://jqlang.github.io/jq/download/**](https://jqlang.github.io/jq/download/)\
\
**VSCode Extension**\
\
[**https://marketplace.visualstudio.com/items?itemName=davidnussio.vscode-jq-playground**](https://marketplace.visualstudio.com/items?itemName=davidnussio.vscode-jq-playground)\
\ <br>

**Download the sample JSON from**

```
https://github.com/gchandra10/jqtutorial
```

Note: This [**JSON file** ](https://www.analyticsvidhya.com/blog/2024/01/understanding-json-loads-and-json-dump-in-python/#:~:text=JSON%20\(JavaScript%20Object%20Notation\)%20is,loads\(\)%20and%20json.?utm_source=Backlink\&utm_medium=SEO)has no root element. '.' is used.

**1. View JSON file in readable format**

```
$ jq '.' sample_nows.json
```

**2. Read the First JSON element / object**

```
$ jq 'first(.[])' sample_nows.json
```

**3. Read the Last JSON element**

```
$ jq 'last(.[])' sample_nows.json
```

**4. Read top 3 JSON elements**

```
$ jq 'limit(3;.[])' sample_nows.json
```

**5. Read 2nd & 3rd element. Remember, Python has the same format. LEFT Side inclusive, RIGHT Side exclusive**

```
$ jq '.[2:4]' sample_nows.json
```

**6. Extract individual values. | Pipeline the output**

```
$ jq '.[] | [.balance,.age]' sample_nows.json
```

**7. Extract individual values and do some calculations**

```
$ jq '.[] | [.age, 65 - .age]' sample_nows.json
```

**8. Return CSV from JSON**

```
$ jq '.[] | [.company, .phone, .address] | @csv ' sample_nows.json
```

**9. Return Tab Separated Values (TSV) from JSON**

```
$ jq '.[] | [.company, .phone, .address] | @tsv ' sample_nows.json
```

**10. Return with custom pipeline delimiter ( | )**

<pre><code><strong>$ jq '.[] | [.company, .phone, .address] | join("|")' sample_nows.json
</strong><strong>
</strong><strong>Pro TIP : Export this result > output.txt and Import to db using bulk import tools like bcp, load data infile
</strong></code></pre>

**11. Convert the number to string and return | delimited result**

```
$ jq '.[] | [.balance,(.age | tostring)] | join("|") ' sample_nows.json
```

**12. Process Array return Name (returns as list / array)**

```
$ jq '.[] | [.friends[].name]' sample_nows.json
```

**or (returns line by line)**

```
$ jq '.friends[].name' sample_nows.json
```

**13. Parse multi level values**

```
$ jq '.[] | [.name.first, .name.last]' sample_nows.json 
(returns as list / array)

$ jq '.[].name.first, .[].name.last' sample_nows.json 
(returns line by line )
```

**14. Query values based on condition, say .index > 2**

```
$ jq 'map(select(.index > 2))' sample_nows.json

$ jq 'map(select(.index > 2)) | .[] | [.index,.balance,.age]' sample_nows.json
```

**15. Sorting Elements**

```
# Sort by Age ASC
$ jq 'sort_by(.age)' sample_nows.json

# Sort by Age DESC
$ jq 'sort_by(-.age)' sample_nows.json

# Sort on multiple keys
$ jq 'sort_by(.age, .index)' sample_nows.json

```

\
Use Cases

```
curl -s https://www.githubstatus.com/api/v2/status.json
```

```
curl -s https://www.githubstatus.com/api/v2/status.json | jq '.'
```

```
curl -s https://www.githubstatus.com/api/v2/status.json | jq '.status'
```
