jq notes

Transforming a object of objects in a CSV file

The program:

#!/bin/sh
set -eu
document="$(cat)"
fields="$(echo "$document" | jq '.[] | keys[]' | sort -u)"
echo "$fields" | paste -d\; -s -
echo "$document" | jq ".[] | .$(echo "$fields" \
	| paste -d'@' -s - | sed 's/@/ + ";" + ./g')" \
	| tr -d \"

Can convert to a CSV file like this:

{
	"foo": {
		"name": "myfoo",
		"number": "5"
	},
	"bar": {
		"name": "mybar",
		"number": "10"
	}
}

Into this:

"name","number"
myfoo;5
number,10

Matrix sync response handling

Printing information of a specific room:

$ jq '.rooms.join."#roomid"'