This page collects some operations you might have to do with JSON files. This is mostly connect to the PMPC campaign where we extensive work with JSON files.
For most operations you need to have jq installed.
Change a user's value
Scenario: You have to unpublicise a signature from the signatures.json file.
The key for the signature publication, which is .included_vars.permissionPub in this example.
The user's email address she used for signing. In this example, it's email@example.com. Also see the matching/finding procedure which makes it very easy
The desired value for the JSON key, in this example no
Execute in Bash Shell:
grep -io "firstname.lastname@example.org" signatures.json # Check whether there are multiple signatures with the same email. # Per user there are 2 entries (one in the mail body, one in include_vars # If there are more than 2, make sure to pick the correct one, and copy it # If 2, copy the mail address (case-sensitivity!) cp signatures.json signatures_bak_$(date +%s).json # copy DB to a safe place # REPLACE in user's mail (email@example.com), desired JSON key (.include_vars.permissionPub), and desired new value (no) jq -c 'map((select(.include_vars.confirm == "firstname.lastname@example.org") | .include_vars.permissionPub) |= "no")' signatures.json > signatures_edit.json # Replace signatures.json if the amount of signatures matches the edited file if [ $(grep -o "timestamp" signatures_edit.json | wc -l) == $(grep -o "timestamp" signatures.json | wc -l) ]; then echo "Amount of JSON entries matches. signatures.json replaced with edited file." mv signatures_edit.json signatures.json fi
Extract all values of a specific user
Scenario: You want to see a full record of a single user (identified by email address).
jq 'map(select(.include_vars.confirm == "email@example.com") | . )' signatures.json
To limit this to the data the user put in the form fields, run:
jq 'map(select(.include_vars.confirm == "firstname.lastname@example.org") | .include_vars )' signatures.json
Extract a specific value of selected users
Scenario: You want to extract all comments of signatories in Germany.
jq 'map(select(.include_vars.country == "Germany") | .include_vars.comment | select(length > 0))' signatures.json
| select(length > 0) ignores empty comments
Find a user with limited information
Scenario: You only have a name of a signatory (example: "Kirschner") and need to find the email or other attributes she subscribed with.
jq 'map(select(.include_vars.name | contains("Kirschner")) | . )' signatures.json
This may show multiple entries. Try to change the key or search query accordingly.
Print all users with a certain attribute
Scenario: You want to print all users who agreed to receive news in a CSV format.
jq -r 'map(select(.include_vars.permissionNews == "yes") | .include_vars | [.name, .confirm]) | . | @csv' signatures.json
Note: match() requires jq >= 1.5. Use it on another computer of your jq is too old.