Using JSON data in form

How to access nested JSON data in a ChronoForms form.

Overview

The issue occurs when trying to directly access a nested value within a JSON object without proper decoding.
Use the correct array syntax to navigate the decoded JSON structure, specifying the array index to retrieve the desired value.

Answered
ChronoForms v5
sz szabob 22 May, 2019
Hi Everyone,

I am using CF5. I have the following data in JSON. How can I access the "amount" value in line 21 from a form ?
sz szabob 22 May, 2019
{
"created": 000000000,
"livemode": false,
"id": "evt_00000000000000",
"type": "checkout.session.completed",
"object": "event",
"request": null,
"pending_webhooks": 1,
"api_version": "2019-03-14",
"data": {
"object": {
"id": "cs_00000000000000",
"object": "checkout.session",
"billing_address_collection": null,
"cancel_url": "https://example.com/cancel",
"client_reference_id": null,
"customer": null,
"customer_email": null,
"display_items": [
{
"amount": 1500,
"currency": "usd",
"custom": {
"description": "Comfortable cotton t-shirt",
"images": null,
"name": "T-shirt"
},
"quantity": 2,
"type": "custom"
}
],
"livemode": false,
"locale": null,
"payment_intent": "pi_00000000000000",
"payment_method_types": [
"card"
],
"subscription": null,
"success_url": "https://example.com/success"
}
}
}
sz szabob 23 May, 2019
Any clue? I am trying the following code but it does not work:
$event->data->object->display_items["amount"];
he healyhatman 23 May, 2019
Need to use json_decode on display_items first
sz szabob 24 May, 2019
Answer
Thanks, I figured it out. This worked:
$event->data->object["display_items"][0]["amount"]
This topic is locked and no more replies can be posted.