jq Useful Commands
This page collects practical jq commands for inspecting and extracting JSON data from APIs, especially large TeslaMate API responses.
Basic usage
Pretty-print a JSON file:
jq '.' details.json
Open pretty-printed JSON with jless:
jq '.' details.json | jless
Read from curl and inspect with jless:
curl -s "$URL" | jq '.' | jless
Inspect top-level structure
Show top-level keys:
jq 'keys' details.json
Show keys under data:
jq '.data | keys' details.json
Show keys under the drive object:
jq '.data.drive | keys' details.json
Check the number of GPS detail records:
jq '.data.drive.drive_details | length' details.json
Extract one nested field
Extract the whole drive_details array:
jq '.data.drive.drive_details' details.json
Extract the first GPS detail point:
jq '.data.drive.drive_details[0]' details.json
Extract the last GPS detail point:
jq '.data.drive.drive_details[-1]' details.json
Extract selected GPS fields
Extract only date, latitude, and longitude from every GPS point:
jq '.data.drive.drive_details[] | {date, latitude, longitude}' details.json
View the extracted points in jless:
jq '.data.drive.drive_details[] | {date, latitude, longitude}' details.json | jless
Return the extracted points as one JSON array:
jq '[.data.drive.drive_details[] | {date, latitude, longitude}]' details.json
Save the extracted GPS points to a new JSON file:
jq '[.data.drive.drive_details[] | {date, latitude, longitude}]' details.json > gps-points.json
Inspect the smaller output file:
jless gps-points.json
Export GPS points as CSV
Export date, latitude, and longitude as CSV:
jq -r '
["date","latitude","longitude"],
(.data.drive.drive_details[] | [.date, .latitude, .longitude])
| @csv
' details.json > gps.csv
Preview the CSV:
head gps.csv
Compact line-by-line GPS format
Print one GPS point per line:
jq -r '.data.drive.drive_details[] | "\(.date) \(.latitude),\(.longitude)"' details.json
Example output:
2026-06-07T11:44:04+08:00 22.593358,113.970244
Filter by time range
Filter records after a given timestamp:
jq '[
.data.drive.drive_details[]
| select(.date >= "2026-06-07T11:44:00+08:00")
| {date, latitude, longitude}
]' details.json
Filter records between two timestamps:
jq '[
.data.drive.drive_details[]
| select(
.date >= "2026-06-07T11:44:00+08:00"
and .date <= "2026-06-07T11:54:00+08:00"
)
| {date, latitude, longitude}
]' details.json
Filter by speed
Show only points where the car was moving:
jq '[
.data.drive.drive_details[]
| select(.speed > 0)
| {date, latitude, longitude, speed}
]' details.json
Show stationary points:
jq '[
.data.drive.drive_details[]
| select(.speed == 0)
| {date, latitude, longitude, speed}
]' details.json
Get start and end GPS points
Extract the first and last GPS point:
jq '{
start: (.data.drive.drive_details[0] | {date, latitude, longitude}),
end: (.data.drive.drive_details[-1] | {date, latitude, longitude})
}' details.json
Count and sample records
Count all GPS points:
jq '.data.drive.drive_details | length' details.json
Show the first 5 GPS points:
jq '.data.drive.drive_details[:5]' details.json
Show the last 5 GPS points:
jq '.data.drive.drive_details[-5:]' details.json
Show every 10th GPS point:
jq '[
.data.drive.drive_details
| to_entries[]
| select(.key % 10 == 0)
| .value
| {date, latitude, longitude}
]' details.json
Build a smaller TeslaMate GPS file
Recommended command for TeslaCam GPS work:
jq '[
.data.drive.drive_details[]
| {
date,
latitude,
longitude,
speed,
power,
odometer
}
]' details.json > teslamate-gps.json
Then inspect it:
jless teslamate-gps.json
Fetch from TeslaMate API and save GPS points
Example using environment variables:
curl -s "$TM_API/api/v1/cars/1/drives/46" \
-H "$TM_AUTH" \
| jq '[
.data.drive.drive_details[]
| {date, latitude, longitude, speed, power, odometer}
]' \
> drive-46-gps.json
Open the result:
jless drive-46-gps.json
Notes
- Use
jqto transform JSON. - Use
jlessto browse JSON interactively. - For very large API responses, extract only the fields you need before opening the data in
jless. - Prefer array output when saving transformed data to a file.