TechDocs/JSON

JSON Procedures

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.

You need:

Execute in Bash Shell:

grep -io "user@fsfe.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 (user@fsfe.org), desired JSON key (.include_vars.permissionPub), and desired new value (no)
jq -c 'map((select(.include_vars.confirm == "user@fsfe.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.

TechDocs/JSON (last edited 2017-09-18 14:14:37 by max.mehl)