Code Lab

Field from a response

EasyJSON and data4 min

A test got the body of an API response as a JSON string. The product is in the field data.product. Write a function getProduct(body) that returns an object with only the name and price of that product.

Examples

getProduct('{"status":200,"data":{"product":{"id":7,"name":"Phone","price":300}}}')

Returns { name: 'Phone', price: 300 }

The `id` field is not in the result.

Source

Loading the editor...
  1. InputgetProduct('{"status":200,"data":{"product":{"id":7,"name":"Phone","price":300}}}')
    Expected{ name: 'Phone', price: 300 }
  2. InputgetProduct('{"status":200,"data":{"product":{"id":12,"name":"Laptop","price":1500,"stock":4}}}')
    Expected{ name: 'Laptop', price: 1500 }

Hidden tests run on Submit.