Parsing Responses
Parsing Responses
JSONPath is a tool that makes it easier to write code that selects and extracts data from JSON. For a complete usage guide, see JSONPath.
Example: Extracting Data from a Cart
In this example, zoom retrieved the cart resource and its corresponding lineitems and total resources. Once the client application receives the response, JSONPath can extract the data you're interested in.
{ "self": { "type": "elasticpath.carts.cart", "uri": "/commerce-legacy/carts/mobee/guz=?zoom=lineitems,total", "href": "http://api.elasticpath.net/cortex/carts/mobee/guz=?zoom=lineitems,total" }, "total-quantity": 3, "links": [ { "rel": "lineitems", "rev": "cart", "type": "elasticpath.collections.links", "uri": "/commerce-legacy/carts/mobee/guz=/lineitems", "href": "http://api.elasticpath.net/cortex/carts/mobee/guz=/lineitems" }, { "rel": "order", "rev": "cart", "type": "elasticpath.orders.order", "uri": "/commerce-legacy/orders/mobee/mu3=", "href": "http://api.elasticpath.net/cortex/orders/mobee/mu3=" }, { "rel": "total", "rev": "cart", "type": "elasticpath.totals.total", "uri": "/commerce-legacy/totals/carts/mobee/guz=", "href": "http://api.elasticpath.net/cortex/totals/carts/mobee/guz=" } ], "_lineitems": [ { "self": { "type": "elasticpath.collections.links", "uri": "/commerce-legacy/carts/mobee/guz=/lineitems", "href": "http://api.elasticpath.net/cortex/carts/mobee/guz=/lineitems" }, "links": [ { "rel": "element", "rev": "list", "type": "elasticpath.carts.line-item", "uri": "/commerce-legacy/carts/mobee/guz=/lineitems/hfq=", "href": "http://api.elasticpath.net/cortex/carts/mobee/guz=/lineitems/hfq=" }, { "rel": "element", "rev": "list", "type": "elasticpath.carts.line-item", "uri": "/commerce-legacy/carts/mobee/guz=/lineitems/gbq=", "href": "http://api.elasticpath.net/cortex/carts/mobee/guz=/lineitems/gbq=" }, { "rel": "cart", "rev": "lineitems", "type": "elasticpath.carts.cart", "uri": "/commerce-legacy/carts/mobee/guz=", "href": "http://api.elasticpath.net/cortex/carts/mobee/guz=" } ] } ], "_total": [ { "self": { "type": "elasticpath.totals.total", "uri": "/commerce-legacy/totals/carts/mobee/guz=", "href": "http://api.elasticpath.net/cortex/totals/carts/mobee/guz=" }, "cost": [ { "amount": 211, "currency": "CAD", "display": "$211.00" } ], "links": [ { "rel": "cart", "rev": "total", "type": "elasticpath.carts.cart", "uri": "/commerce-legacy/carts/mobee/guz=", "href": "http://api.elasticpath.net/cortex/carts/mobee/guz=" } ] } ] }
Example | JSONPath | Result |
---|---|---|
Extract the total-quantity child property. |
$total-quantity | [ 3 ] |
Extract every quantity value nested in other properties. | $..quantity | [ 2, 1 ] |
Extract the first element of the total cost array. | $_total..cost[0] | [ { "amount": 211, "currency": "CAD", "display": "$211.00" } ] |