Created
May 20, 2023 15:02
-
-
Save kejadlen/606a4e63ba46367b55359b10ea2dcfec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://github.com/qmacro/level-up-your-json-fu-with-jq/blob/main/talknotes.md | |
pbpaste | from json | |
pbpaste | save environment.json | |
# jq '.availableEnvironments|length' environments.json | |
open environment.json | get availableEnvironments | length | |
# jq -r '.[].name' offerings.json | |
open offerings.json | get name | to text | |
# jq 'map({(.name): .roleReferences|length})|add' collections.json | |
open collections.json | reduce -f {} {|it,acc| $acc | merge {$it.name: ($it.roleReferences | length)}} | |
# jq '.datacenters[]|select(.iaasProvider == "AZURE").displayName' regions.json | |
open regions.json | get datacenters | where iaasProvider == AZURE | get displayName | |
# jiq | |
echo '' | fzf-tmux -p 80% --preview 'nu -c "open regions.json | $(echo {q})"' | |
# cat threestrings.dat | jq '.' | |
open threestrings.dat | lines | each {|| from json} | |
# cat threestrings.dat | jq 'length' | |
open threestrings.dat | lines | each {|| from json | size | get chars} | |
# cat threestrings.dat | jq -s '.' | |
open threestrings.dat | lines | |
# cat threestrings.dat | jq -s 'length' | |
open threestrings.dat | lines | length | |
# cat threestrings.dat | jq 'select(startswith("s"))|ascii_upcase' | |
open threestrings.dat | lines | each {|| from json} | where {|| str starts-with s} | str upcase | |
# cat threestrings.dat | jq -r -s '@csv' | |
open threestrings.dat | lines | each {|| from json} | wrap x | transpose -i | to csv -n | |
# cat threestrings.dat | jq -s '{words:., chars:map(length)|add}' | |
open threestrings.dat | lines | each {|| from json} | {words: $in, chars: ($in | size | get chars | math sum)} | |
# jq '.sessions[].speaker' btpcon.json | |
open btpcon.json | get sessions.speaker | |
# jq '.sessions | last | [.title, .starttime[:2]]' btpcon.json | |
open btpcon.json | get sessions | last | select title starttime | update starttime {|x| $x.starttime | str substring 0..2} | |
# def ampm: | |
# if .starttime[:2] | tonumber < 12 | |
# then "morning" | |
# else "afternoon" | |
# end | |
# ; | |
def ampm [session] { | |
if ($session.starttime | str substring 0..2 | into int ) < 12 { | |
"morning" | |
} else { | |
"afternoon" | |
} | |
} | |
# jq 'import "lib" as lib; .sessions | map(.when = lib::ampm)' btpcon.json | |
open btpcon.json | get sessions | each {|x| merge {when: (ampm $x)}} | |
# cat btpcon.json \ | |
# | jq ' | |
# import "lib" as lib; | |
# .sessions | |
# | map(.when = lib::ampm) | |
# | group_by(.when) | |
# | reverse | |
# | map({ | |
# (first.when): map(.title) | |
# }) | |
# ' | |
# this one returns the same structure as the jq snippet (single-element array) | |
open btpcon.json | get sessions | each {|x| merge {when: (ampm $x)}} | group-by when | update cells {|| get title} | |
# to unwrap the array | |
open btpcon.json | get sessions | each {|x| merge {when: (ampm $x)}} | group-by when | update cells {|| get title} | get 0 | |
# an alternative? | |
open btpcon.json | get sessions | each {|x| merge {when: (ampm $x)}} | group-by when | transpose key value | each {|| select key value.title } | transpose -r | into record | |
# cat btpcon.json \ | |
# | jq ' | |
# import "lib" as lib; | |
# .sessions[] | |
# | select(.type=="talk" and lib::ampm=="afternoon") | |
# | .speaker | |
# | split(" ") | |
# | [last, first] | |
# | join(", ") | |
# ' | |
open btpcon.json | get sessions | where {|x| ($x.type == talk) and ((ampm $x) == afternoon)} | get speaker | each {|| split words | reverse | str join ", "} |
You may also be able to use each { ... }
instead of each {|| ... }
in more recent Nushell versions.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've looked through these and they seem pretty idiomatic to me.
There are some that could be smaller like this
could be
but generally this is a good port. Nice work!
Another thing that I'd add is that if we're entirely focused on parsing json, there are two plugins that could help and specialize in parsing json.
query json
and is based on gjsonjson path
and is based on the json path specification