---
title: jq vs Nushell
---
# jq vs Nushell
Both [`jq`](https://jqlang.github.io/jq/) and `nu` have the ability to transform data in a composable way. This cookbook will walk you through common data manipulation tasks with the aim of building a solid mental model for using Nushell effectively.
All examples will stick to JSON to keep parity between examples.
## Consuming JSON
Let's start with the basics: consuming a JSON string.
In `jq`, inputs are always expected to be JSON so we simply do:
```sh
echo '{"title": "jq vs Nushell", "publication_date": "2023-11-20"}' | jq -r '.'
```
In `nu`, we need to be explicit because Nushell has a wider range of input choices:
```nu
'{"title": "jq vs Nushell", "publication_date": "2023-11-20"}'
| from json
# => ╭──────────────────┬───────────────╮
# => │ title │ jq vs Nushell │
# => │ publication_date │ 2023-11-20 │
# => ╰──────────────────┴───────────────╯
```
The output for `jq` is a JSON string whereas in `nu` it's a Nushell value. To get the output of any pipeline as JSON, simply apply a [`to json`](/commands/docs/to_json.html) at the end:
```nu
'[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]'
| from json
| to json
```
Output:
```json
{
"title": "jq vs Nushell",
"publication_date": "2023-11-20"
}
```
When your JSON data is stored in a file, you can use [open](/commands/docs/open.html) instead of [from json](/commands/docs/from_json.html).
Before we get into the examples, the following glossary can help familiarise yourself with how Nushell data types map to jq data types.
| Nushell | jq |
| ------- | -------------- |
| integer | number |
| decimal | number |
| string | string |
| boolean | boolean |
| null | null |
| list | array |
| record | object |
| table | not applicable |
| command | filter |
## Basic operations
### Selecting values
In `jq`, to get the value from an object we do:
```sh
echo '{"name": "Alice", "age": 30}' | jq -r '.name'
```
In `nu` we do:
```nu
'{"name": "Alice", "age": 30}' | from json | get name
# => Alice
```
### Filtering lists
In `jq`, to filter an array we do:
```sh
echo '[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]' |
jq -r '.[] | select(.age > 28)'
```
In `nu` we do:
```nu
'[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]'
| from json
| where age > 28
# => ╭───┬───────┬─────╮
# => │ # │ name │ age │
# => ├───┼───────┼─────┤
# => │ 0 │ Alice │ 30 │
# => ╰───┴───────┴─────╯
```
### Mapping over lists
In `jq`, to map over a list we do:
```sh
echo '[1, 2, 3, 4, 5]' |
jq -r 'map(. * 2)'
```
In `nu` we do:
```nu
'[1, 2, 3, 4, 5]'
| from json
| each { |x| $x * 2 }
# => ╭───┬────╮
# => │ 0 │ 2 │
# => │ 1 │ 4 │
# => │ 2 │ 6 │
# => │ 3 │ 8 │
# => │ 4 │ 10 │
# => ╰───┴────╯
```
Note that you can rely on the `$in` auto-binding for a slightly more compact block:
```nu
'[1, 2, 3, 4, 5]'
| from json
| each { $in * 2 }
```
### Mapping over records
In `jq`, to map over a record we do:
```sh
echo '{"items": [{"name": "Apple", "price": 1}, {"name": "Banana", "price": 0.5}]}' |
jq -r '.items | map({(.name): (.price * 2)}) | add'
```
In `nu` we do:
```nu
'{"items": [{"name": "Apple", "price": 1}, {"name": "Banana", "price": 0.5}]}'
| from json
| get items
| update price {|row| $row.price * 2}
# => ╭───┬────────┬───────╮
# => │ # │ name │ price │
# => ├───┼────────┼───────┤
# => │ 0 │ Apple │ 2 │
# => │ 1 │ Banana │ 1.00 │
# => ╰───┴────────┴───────╯
```
In this case nu does not require creating new records because we can leverage the fact that a list of records is a table. However, in other situations it might be required as we have seen in [Composing records](#composing-records).
### Sorting lists
In `jq`, to sort a list we do:
```sh
echo '[3, 1, 4, 2, 5]' |
jq -r 'sort'
```
In `nu` we do:
```nu
'[3, 1, 4, 2, 5]'
| from json
| sort
# => ╭───┬───╮
# => │ 0 │ 1 │
# => │ 1 │ 2 │
# => │ 2 │ 3 │