1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Testing out using nushell as a swiss army knife for data manipulation by
translating these jq exercises to nushell:

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 ", "}
```