Introduction
I’m a beginner in both Python and Zapier, and I’m encountering some hurdles. In my MySQL database, I have a column dedicated to storing JSON array data. I’ve managed to access this field through Zapier, but I’m having trouble with extracting specific values from the JSON format.
Objective
My goal is to extract a value labeled ‘postcode’ from this JSON array using Python code within my Zapier automation. The JSON structure is somewhat complex with nested objects, and I only want to isolate that specific piece of information.
Current status
I’ve tried to write some simple Python code for this task, but I’m not seeing the results I expected. Although the JSON data appears to be correctly fetched from the database, I’m facing challenges when attempting to parse it and retrieve the postcode value.
Example of JSON layout
When decoded, the data resembles the following:
[
{
"address_info": {
"street": "123 Main St",
"postcode": "12345",
"city": "Sample City"
}
}
]
My code trial
import json
# Access JSON data from MySQL field
raw_data = input_data['mysql_json_field']
# Attempt to parse and extract the postcode
try:
parsed_data = json.loads(raw_data)
target_postcode = parsed_data[0]['address_info']['postcode']
output = {'extracted_postcode': target_postcode}
except Exception as e:
output = {'error': str(e)}
I would really appreciate any help on what I might be doing incorrectly. Any advice on managing JSON arrays in Zapier would be hugely valuable.