# JSONPath in Batches

<p>
    ChargeOver supports using JSONPath for referencing one payload from within another payload, inside of a single batch/bulk request.
</p>
<p>
    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 <code>id</code> value returned from executing the first payload (adding the customer) so that you could use this <code>id</code> value in the second payload (adding the invoice).
</p>
<p>
    JSONPath referencing support makes this possible by letting payloads refer to response values from other payloads.
</p>
<p>
    In the example to the right, you can see that our second request (adding the invoice) uses a special key instead of the normal <code>customer_id</code> key. The invoice add payload refers to <code>customer_jsonpath</code> and the value is an identifier (<code>the_customer</code>), followed by a JSONPath expression (<code>$.response.id</code>).
</p>
<p>
    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, <code>customer_jsonpath</code> will be replaced by the returned <code>id</code> value from the preceeding call to create the customer.
</p>
<p>
    In other words, <code>"customer_jsonpath": "the_customer:$.response.id"</code> essentially means "replace this with the <code>customer_id</code> value returned from the payload named <code>the_customer</code>".
</p>

<p>
    ```php title="API Request"
    POST /api/v3/_bulk
    ```
    ```json title="API Request Body"
    [
        {
            "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
        }
    ]
    ```

    ```json title="API RESPONSE BODY"
    {
        "code": 200,
        "status": "OK",
        "message": "",
        "response": {
            "_bulk": [
                {
                    "code": 201,
                    "status": "OK",
                    "message": "",
                    "response": {
                        "id": 6
                    }
                },
                {
                    "code": 201,
                    "status": "OK",
                    "message": "",
                    "response": {
                        "id": 10005
                    }
                }
            ]
        }
    }
    ```
</p>
