TIL #1: how to sort JSON with jq
Published:
For discu.eu I manage at least twenty bots and each one has a separate Firefox container.
I created the containers overtime without a particular order. Now each time I need to open a new tab in a specific container it takes me some time to pinpoint the one I need (for example the one for Rust). So I needed to sort them by name to find them faster.
Firefox saves the list in the containers.json
file under
the profile directory, which can be found by opening
about:support
. So I opened the file with nvim
and run
:%! jq '.identities |= sort_by(.name|ascii_upcase)'
Which does the following:
:
starts the command mode of nvim%
selects the whole file!
replaces the selection with the output of the following shell commandjq
is an amazing cli JSON processor. The following string does the following:.identities
selects the key from the main JSON object.identities
is an array|=
the selection is passed to the following filter and updated “in place”, everything else remains as issort_by(.name|ascii_upcase)
sorts an array of objects using the keyname
, but first convertsname
to uppercase to simulate case insensitive sorting
Here’s my containers.json
:
{
"version": 4,
"lastUserContextId": 58,
"identities": [
{
"userContextId": 48,
"public": true,
"icon": "gift",
"color": "red",
"name": "apl"
},
{
"userContextId": 14,
"public": true,
"icon": "food",
"color": "pink",
"name": "c&cpp"
},
{
"userContextId": 20,
"public": true,
"icon": "dollar",
"color": "green",
"name": "compsci"
},
{
"userContextId": 21,
"public": true,
"icon": "cart",
"color": "yellow",
"name": "devops"
},
{
"userContextId": 57,
"public": true,
"icon": "circle",
"color": "purple",
"name": "discueu"
},
{
"userContextId": 16,
"public": true,
"icon": "pet",
"color": "toolbar",
"name": "erlang&elixir"
},
{
"userContextId": 18,
"public": true,
"icon": "fingerprint",
"color": "blue",
"name": "golang"
},
{
"userContextId": 9,
"public": true,
"icon": "cart",
"color": "yellow",
"name": "Google"
},
{
"userContextId": 19,
"public": true,
"icon": "briefcase",
"color": "turquoise",
"name": "haskell"
},
{
"userContextId": 13,
"public": true,
"icon": "gift",
"color": "red",
"name": "hn"
},
{
"userContextId": 46,
"public": true,
"icon": "cart",
"color": "yellow",
"name": "java"
},
{
"userContextId": 42,
"public": true,
"icon": "fruit",
"color": "purple",
"name": "laarc"
},
{
"userContextId": 15,
"public": true,
"icon": "fruit",
"color": "purple",
"name": "lisp&scheme"
},
{
"userContextId": 44,
"public": true,
"icon": "fingerprint",
"color": "blue",
"name": "nim"
},
{
"userContextId": 47,
"public": true,
"icon": "vacation",
"color": "orange",
"name": "php"
},
{
"userContextId": 10,
"public": true,
"icon": "vacation",
"color": "orange",
"name": "programmingdiscu"
},
{
"userContextId": 7,
"public": true,
"icon": "briefcase",
"color": "turquoise",
"name": "PythonDiscussions"
},
{
"userContextId": 58,
"public": true,
"icon": "cart",
"color": "red",
"name": "quotes"
},
{
"userContextId": 30,
"public": true,
"icon": "gift",
"color": "pink",
"name": "routinecheck"
},
{
"userContextId": 17,
"public": true,
"icon": "tree",
"color": "blue",
"name": "ruby"
},
{
"userContextId": 6,
"public": true,
"icon": "fingerprint",
"color": "blue",
"name": "RustDiscussions"
},
{
"userContextId": 25,
"public": true,
"icon": "fence",
"color": "purple",
"name": "RWB"
},
{
"userContextId": 12,
"public": true,
"icon": "chill",
"color": "toolbar",
"name": "social media"
},
{
"userContextId": 56,
"public": true,
"icon": "fence",
"color": "toolbar",
"name": "tata"
},
{
"userContextId": 23,
"public": true,
"icon": "vacation",
"color": "orange",
"name": "unix"
},
{
"userContextId": 5,
"public": false,
"icon": "",
"color": "",
"name": "userContextIdInternal.thumbnail",
"accessKey": ""
},
{
"userContextId": 4294967295,
"public": false,
"icon": "",
"color": "",
"name": "userContextIdInternal.webextStorageLocal",
"accessKey": ""
},
{
"userContextId": 43,
"public": true,
"icon": "pet",
"color": "toolbar",
"name": "webdev"
},
{
"userContextId": 45,
"public": true,
"icon": "briefcase",
"color": "turquoise",
"name": "zig"
}
]
}
P.S. I just learned that the command mode can be opened with
q:
. This way vim motions can be used to edit the
command.