Skip to content

Instantly share code, notes, and snippets.

@kejadlen
Created May 20, 2023 15:02
Show Gist options
  • Select an option

  • Save kejadlen/606a4e63ba46367b55359b10ea2dcfec to your computer and use it in GitHub Desktop.

Select an option

Save kejadlen/606a4e63ba46367b55359b10ea2dcfec to your computer and use it in GitHub Desktop.
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 ", "}
@fdncred

fdncred commented May 24, 2023

Copy link
Copy Markdown

I've looked through these and they seem pretty idiomatic to me.

There are some that could be smaller like this

open threestrings.dat | lines | each {|| from json | size | get chars}

could be

open threestrings.dat | lines | each {|| from json | str length}

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.

@sophiajt

Copy link
Copy Markdown

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