mirror of
https://github.com/jbranchaud/til
synced 2026-01-21 07:58:02 +00:00
Compare commits
2 Commits
0dda4ab55a
...
12178cf153
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
12178cf153 | ||
|
|
f7313218c1 |
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1286 TILs and counting..._
|
_1288 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -485,6 +485,8 @@ _1286 TILs and counting..._
|
|||||||
|
|
||||||
### jq
|
### jq
|
||||||
|
|
||||||
|
- [Count Each Collection In A JSON Object](jq/count-each-collection-in-a-json-object.md)
|
||||||
|
- [Count The Number Of Things In A JSON File](jq/count-the-number-of-things-in-a-json-file.md)
|
||||||
- [Extract A List Of Values](jq/extract-a-list-of-values.md)
|
- [Extract A List Of Values](jq/extract-a-list-of-values.md)
|
||||||
- [Find All Objects With A Matching Key Value Pair](jq/find-all-objects-with-a-matching-key-value-pair.md)
|
- [Find All Objects With A Matching Key Value Pair](jq/find-all-objects-with-a-matching-key-value-pair.md)
|
||||||
- [Reduce Object To Just Entries Of A Specific Type](jq/reduce-object-to-just-entries-of-a-specific-type.md)
|
- [Reduce Object To Just Entries Of A Specific Type](jq/reduce-object-to-just-entries-of-a-specific-type.md)
|
||||||
|
|||||||
34
jq/count-each-collection-in-a-json-object.md
Normal file
34
jq/count-each-collection-in-a-json-object.md
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
# Count Each Collection In A JSON Object
|
||||||
|
|
||||||
|
Let's say your JSON file is an object that represents several different
|
||||||
|
collections (arrays) of data.
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"users": [ ... ],
|
||||||
|
"orders": [ ... ],
|
||||||
|
"carts": [ ... ]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
We can get a nice summary of the counts of those collections using
|
||||||
|
[`jq`](https://stedolan.github.io/jq/). We can do that with the [`with_entries`
|
||||||
|
function](https://stedolan.github.io/jq/manual/#to_entries,from_entries,with_entries).
|
||||||
|
We preserve the key (name) of each collection and then process each list with
|
||||||
|
the `length` function.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
jq '. | with_entries({ "key": .key, "value": (.value | length)})' data.json
|
||||||
|
{
|
||||||
|
"users": 1234,
|
||||||
|
"orders": 5432,
|
||||||
|
"carts": 89
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The `with_entries` function essentially maps over each key-value pair
|
||||||
|
processing it with the given expression. It will then convert that `{"key":
|
||||||
|
some_key, "value": 123}` mapping back into a key-value pair that gets combined
|
||||||
|
with all the others.
|
||||||
|
|
||||||
|
[source](https://til.simonwillison.net/jq/flatten-nested-json-objects-jq)
|
||||||
43
jq/count-the-number-of-things-in-a-json-file.md
Normal file
43
jq/count-the-number-of-things-in-a-json-file.md
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
# Count The Number Of Things In A JSON File
|
||||||
|
|
||||||
|
JQ is a great tool for finding out the number of things in a JSON file.
|
||||||
|
|
||||||
|
If the top-level contents of the JSON is a list, then you can pipe it directly
|
||||||
|
to the [`length` function](https://stedolan.github.io/jq/manual/#length).
|
||||||
|
|
||||||
|
```bash
|
||||||
|
// [1, 2, {"three": 4}]
|
||||||
|
$ jq '. | length' data.json
|
||||||
|
3
|
||||||
|
```
|
||||||
|
|
||||||
|
It works the same for counting the number of entries (key-value pairs) in a
|
||||||
|
top-level JSON object.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
// { "hello": "world", "list": [1,2,3] }
|
||||||
|
$ jq '. | length' data.json
|
||||||
|
2
|
||||||
|
```
|
||||||
|
|
||||||
|
If you are trying to get the count of a nested value, navigate to it and then
|
||||||
|
pipe that to `length`.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
// { "hello": "world", "list": [1,2,3] }
|
||||||
|
$ jq '.list | length' data.json
|
||||||
|
3
|
||||||
|
```
|
||||||
|
|
||||||
|
You can even count each value in a JSON object by transforming it into an array
|
||||||
|
of the values with `[]`.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
// { "hello": "world", "list": [1,2,3] }
|
||||||
|
$ jq '.[] | length' data.json
|
||||||
|
5
|
||||||
|
3
|
||||||
|
```
|
||||||
|
|
||||||
|
Notice, the length of `"world"` is `5` characters and the length of `[1,2,3]`
|
||||||
|
is `3` elements.
|
||||||
Reference in New Issue
Block a user