Unit 8: APIs and how modern apps talk · Lesson 1 of 5 · 18 min
JSON APIs
The shop's mobile app talks to a JSON API. jq turns its answers into something you can read and query.
mission
- You are
- The analyst reviewing the shop's API.
- Situation
- The mobile app uses
/api/v1/. Before reviewing it, you need to read its answers fluently. - Goal
- Query the product endpoint and shape the JSON with jq.
- Hand in
- A one-line-per-product listing built from the API.
An API as a vending machine
Raw JSON
curl -s http://127.0.0.1:8000/api/v1/products
[{"id": 1, "name": "Padlock", "price": 12.5}, {"id": 2, "name": "Cable lock", "price": 18.0}, {"id": 3, "name": "Key safe", "price": 34.0}]
you should see One long line. Fine for programs, hard for people.
Pretty print
curl -s http://127.0.0.1:8000/api/v1/products | jq .
[
{
"id": 1,
"name": "Padlock",
"price": 12.5
},
{
...
you should see An array of objects, one per product.
Pick and filter
curl -s http://127.0.0.1:8000/api/v1/products | jq 'length' 3 curl -s http://127.0.0.1:8000/api/v1/products | jq -r '.[] | select(.price > 15) | .name' Cable lock Key safe
you should see Three products, two of them over 15.
Shape a report line
curl -s http://127.0.0.1:8000/api/v1/products | jq -r '.[] | "\(.id) \(.name) \(.price)"'
1 Padlock 12.5
2 Cable lock 18.0
3 Key safe 34.0
you should see Plain text you can paste into notes or feed to other tools.
defender tip API reviews start with an inventory: every endpoint, its method, whether it needs a token and which fields it returns.
What a defender takes away
- 1APIs answer in JSON; jq is the grep and cut of JSON.
- 2
jq .to read,select()to filter, string templates to report. - 3Inventory endpoints and fields before reviewing anything else.
Questions
0 of 2 solved
Answer each one from what you find on your lab machine.