JSONPath in Batches
ChargeOver supports using JSONPath for referencing one payload from within another payload, inside of a single batch/bulk request.
The classic use-case for JSONPath support in the bulk/batch API is trying to create a customer and an invoice for that customer in a single bulk/batch request. Without JSONPath references, this would not be possible to do in single bulk/batch request because you would need to know the id
value returned from executing the first payload (adding the customer) so that you could use this id
value in the second payload (adding the invoice).
JSONPath referencing support makes this possible by letting payloads refer to response values from other payloads.
In the example to the right, you can see that our second request (adding the invoice) uses a special key instead of the normal customer_id
key. The invoice add payload refers to customer_jsonpath
and the value is an identifier (the_customer
), followed by a JSONPath expression ($.response.id
).
The identifier serves to identify which previous batch/bulk object to refer to. The JSONPath expression tells us which value to fetch from that bulk/batch object response. In this case, customer_jsonpath
will be replaced by the returned id
value from the preceeding call to create the customer.
In other words, "customer_jsonpath": "the_customer:$.response.id"
essentially means "replace this with the customer_id
value returned from the payload named the_customer
".
POST /api/v3/_bulk
[
{
"request_method": "POST",
"uri": "\/api\/v3\/customer",
"payload": {
"company": "My test JSONPath customer"
},
"id": "the_customer"
},
{
"request_method": "POST",
"uri": "\/api\/v3\/invoice",
"payload": {
"customer_jsonpath": "the_customer:$.response.id",
"line_items": [
{
"item_id": 1,
"line_rate": 2.95,
"line_quantity": 2
}
]
},
"id": null
}
]
{
"code": 200,
"status": "OK",
"message": "",
"response": {
"_bulk": [
{
"code": 201,
"status": "OK",
"message": "",
"response": {
"id": 6
}
},
{
"code": 201,
"status": "OK",
"message": "",
"response": {
"id": 10005
}
}
]
}
}