How do I correctly to import an existing Data Action with complex JSON mappings into Terraform state?
I am attempting to import a resource using terraform import, but the provider fails to parse the nested configuration from the API response. The /api/v2/flows/dataactions/{dataActionId} endpoint returns a payload that the provider cannot map to the state file correctly.
Error: expected mapping to have a single JSON object, got array
Is there a specific flag or manual state edit required for these resources?
The root cause here is the Terraform provider expecting a flattened or escaped JSON string for certain complex mapping fields, while the API returns a structured object. The provider’s import logic struggles with deeply nested JSON in Data Actions, specifically the mapping or input/output definitions. You cannot rely on a simple terraform import for these resources without pre-processing the state or using a manual import workaround.
Here is the robust method to handle this:
- Export the Resource via API: Use the PureCloud SDK or a direct API call to get the raw JSON. Do not use the Terraform provider for this step.
curl -X GET "https://api.mypurecloud.com/api/v2/flows/dataactions/{dataActionId}" \
-H "Authorization: Bearer $ACCESS_TOKEN" > dataaction_raw.json
- Flatten Nested JSON for State: The Terraform state file requires the JSON payload to be a string if it’s not mapped to specific sub-resources. You must escape the JSON object into a single string value for the relevant field in the
.tfstate file.
# Example: Convert the 'mapping' object to an escaped string
python3 -c "import json, sys; print(json.dumps(json.load(sys.stdin)['mapping']))" < dataaction_raw.json > mapping_string.txt
- Manual State Injection: Instead of
terraform import, use terraform state push with a manually constructed state snippet, or edit the existing state file directly. Ensure the mapping field is a string containing the escaped JSON.
resource "genesyscloud_flow_dataaction" "my_action" {
name = "My Data Action"
# Ensure this field is a stringified JSON if the provider expects it
mapping = "{\"field1\": \"value1\", \"nested\": {\"key\": \"val\"}}"
}
- Verify with Plan: Run
terraform plan to ensure no drift. If the provider still complains, check if the specific version of the genesyscloud provider supports the nested structure natively. If not, pin to a version that handles JSON escaping more gracefully or open a feature request.
This approach bypasses the parser error by feeding the state exactly what the provider expects.