Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a curl to an API and get an specific field from the JSON

I'm doing a curl like the following

curl -X GET "https://api.mercadolibre.com/items/MLA511127356"

and the response is a JSON, like for example this one:

"id": "MLA511127356",
  "site_id": "MLA",
  "title": "Item De Testeo, Por Favor No Ofertar --kc:off",
  "subtitle": null,
  "seller_id": "160252486",
  "category_id": "MLA4967",
  "official_store_id": null,
  "price": 10,
  "base_price": 10,
  "original_price": null,
  "currency_id": "ARS",
  "initial_quantity": 16,

Is there a simple way to do this?

Thank you!

like image 274
3fff Avatar asked Sep 10 '14 15:09

3fff


People also ask

How do I get JSON with Curl?

How do I get JSON with Curl? To get JSON with Curl, you need to make an HTTP GET request and provide the Accept: application/json request header. The application/json request header is passed to the server with the curl -H command-line option and tells the server that the client is expecting JSON in response.

What is the GET method in curl?

The GET method requests a specific resource from the server. GET is the default method when making HTTP requests with curl. Here is an example of making a GET request to the JSONPlaceholder API to a JSON representation of all posts: To filter the results use query params:

Is it possible to post a curl request from an API?

Because for posting a cURL request you definitely need an URL where the API is installed, but not to reply to that request because the call can come from various sources or webpages. I have successfully used cURL to request data from different APIs but of course, those APIs were already existing.

How do I get JSON from a REST API?

To receive JSON from a REST API endpoint, you must send an HTTP GET request to the REST API server and provide an Accept: application/json request header. The Accept header tells the REST API server that the API client expects JSON. In this REST API GET example, we make a GET request to the ReqBin echo REST API endpoint.


1 Answers

By using jq you could parse the json data instead of the text based parsing.

curl -X GET "https://api.mercadolibre.com/items/MLA511127356" | jq '.[].id'
like image 109
visibilityspots Avatar answered Oct 18 '22 14:10

visibilityspots