### 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 │
# => │ 3 │ 4 │
# => │ 4 │ 5 │
# => ╰───┴───╯
```
### Filtering distinct values in a list
In `jq`, to filter a list keeping unique values we do:
```sh
echo '[1, 2, 2, 3, 4, 4, 5]' |
jq -r 'unique'
```
In `nu` we do:
```nu
'[1, 2, 2, 3, 4, 4, 5]'
| from json
| uniq
# => ╭───┬───╮
# => │ 0 │ 1 │
# => │ 1 │ 2 │
# => │ 2 │ 3 │
# => │ 3 │ 4 │
# => │ 4 │ 5 │
# => ╰───┴───╯
```
### Combining filters
In `jq`, to combine filters we do:
```sh
echo '[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]' |
jq -r '.[] | select(.age > 28) | .name'
```
In `nu` we do:
```nu
'[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]'
| from json
| where age > 28
| get name
# => ╭───┬───────╮
# => │ 0 │ Alice │
# => ╰───┴───────╯
```
### Splitting strings
In `jq`, to split a string we do:
```sh
echo '{"name": "Alice Smith"}' |
jq -r '.name | split(" ") | .[0]'
```
In `nu` we do:
```nu
'{"name": "Alice Smith"}'
| from json
| get name
| split words
| get 0
# => Alice
```
### Conditional logic
In `jq` to work with `if` expressions we do:
```sh
echo '{"name": "Alice", "age": 30}' |
jq -r 'if .age > 18 then "Adult" else "Child" end'
```
In `nu` we do:
```nu
'{"name": "Alice", "age": 30}'
| from json
| if $in.age > 18 { "Adult" } else { "Child" }
# => Adult
```
### Handling `null` values
In `jq`, to filter out `null` values we do:
```sh
echo '[1, null, 3, null, 5]' |
jq -r 'map(select(. != null))'
```
In `nu` we do:
```nu
'[1, null, 3, null, 5]'
| from json
| where { $in != null }
# => ╭───┬───╮
# => │ 0 │ 1 │
# => │ 1 │ 3 │
# => │ 2 │ 5 │
# => ╰───┴───╯
```
Alternatively, you can use [`compact`](/commands/docs/compact.html):
```nu
'[1, null, 3, null, 5]'
| from json
| compact
```
### Formatting output
In `jq`, to output a formatted string we do:
```sh
echo '{"name": "Alice", "age": 30}' |
jq -r "Name: \(.name), Age: \(.age)"
```
In `nu` we do:
```nu
'{"name": "Alice", "age": 30}'
| from json
| items { |key, value| ["Name" $value] | str join ": " }
| str join ", "
# => Name: Alice, Name: 30
```
This approach is a bit involved but if we [install the full version](https://github.com/nushell/nushell/releases) which includes the _extra commands_ we can benefit from the [`format`](/commands/docs/format.html):
```nu
'{"name": "Alice", "age": 30}'
| from json
| format "Name: {name}, Age: {age}"
```
### Composing records
In `jq`, to compose a new JSON object (akin to a record in Nushell) we do:
```sh
echo '{"name": "Alice", "age": 30}' |
jq -r '{name: .name, age: (.age + 5)}'
```
In `nu` we do:
```nu
'{"name": "Alice", "age": 30}'
| from json
| {name: $in.name, age: ($in.age + 5)}
# => ╭──────┬───────╮
# => │ name │ Alice │
# => │ age │ 35 │
# => ╰──────┴───────╯
```
## Dealing with nested items
### Filtering nested items
In `jq`, to recursively filter a tree structure we do:
```sh
echo '{"data": {"value": 42, "nested": {"value": 24}}}' |
jq -r '.. | .value?'
```
In `nu`, there is no built-in command to achieve this, however, we can define our own reusable commands. See the [Appendix: Custom commands](#appendix-custom-commands) for an implementation of the command `cherry-pick` shown in the example below.