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 a 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.country == "Germany") | [.include_vars.name, .include_vars.comment])' signatures.json > signatures_germany.json
This may show multiple entries. Try to change the key or search query accordingly.
Use a JSON2CSV converter to create comma-separated values which are easier to use. Think about using an offline converter if you handle sensitive data.
Note: match() requires jq >= 1.5. Use it on another computer of your jq is too old.