Extract JSON config variables from the Airflow UI

One way to configure Apache Airflow is with “Variables” that can be modified programmatically or via the Airflow UI.

You can import the variables from a JSON file, again via the API or by uploading the file in the UI. Sensibly, Airflow does not provide a way to export these variables back out to a JSON file.

However, if you find yourself in the situation where Airflow holds the correct values and you’re not confident you’ve got them stored consistently elsewhere, you might end up having to manually copy the variables back out of the Airflow UI.

This situation is not ideal, and is also labourious. The little JS snippet below at least makes it quicker to extract the variables from the Airflow UI to a JSON blob:

const config = {};
document.querySelectorAll('tbody > tr').forEach((row) => {
    const keyCell = row.querySelector('.col-key');
    const valueCell = row.querySelector('.col-val');
    config[keyCell.innerText.trim()] = valueCell.innerText.trim();
});
console.log(JSON.stringify(config, null, 4));

You can try chucking that in the browser console to see if it works.

Some caveats:

Once you’ve got your variables back out, make sure to store them properly this time so you don’t have to do this again!


Tech mentioned