jq for Apify Datasets - Transform, Filter & Reshape JSON
Pricing
from $0.08 / 1,000 items
jq for Apify Datasets - Transform, Filter & Reshape JSON
Real jq 1.8 over any Apify dataset, inline JSON or a JSON / JSON-lines link. Add it as an Integration and every scraper run comes out filtered and reshaped, with only-new dedupe across runs. Get dataset items, CSV or JSON.
Pricing
from $0.08 / 1,000 items
Rating
0.0
(0)
Developer
Northbell
Maintained by CommunityActor stats
0
Bookmarked
2
Total users
1
Monthly active users
a day ago
Last modified
Categories
Features
Share
Run a real jq program over your scraped data. Give it a dataset (or inline JSON, or a link to a JSON file), write a jq program, and get the results back as dataset items, and optionally as a CSV or JSON file. It is built to run right after a scraper: filter rows, rename and flatten fields, turn text into numbers, dedupe, group and count, and skip items you already have.
The engine is jq 1.8 itself (compiled to WebAssembly), so every jq program you know from the command line or jqplay works the same here.
Try it now — the input is already filled in. Press Start. It runs select(.price != null) | {title, price: (.price|tonumber), url} over 4 example products, one of which has no price. In our own local run with this input: 4 items in, 3 items out, about 2 seconds from start to finish (the jq work itself took 0.2 seconds). Cost: 3 output items = $0.0003 (plus $0.00005 for the run start).
| Event | Price | When |
|---|---|---|
item-out | $0.0001 | per output item written to the dataset ($0.10 per 1,000) |
apify-actor-start | $0.00005 | once per run |
- You pay only for items that are written. Items dropped by your program, results skipped by "Only new items" and items that fail with a jq error are free.
- Set "Maximum total charge" on the run as your cap. The Actor checks the limit before it writes each batch and stops cleanly when the next results do not fit.
Run it after your scraper
Use it as an Integration so every scraper run is cleaned up automatically:
- In Apify Console, open your scraper and go to Integrations.
- Add Run Actor (on "Run succeeded") and pick this Actor.
- Use an input like this:
{"datasetId": "{{resource.defaultDatasetId}}","program": "select(.price != null) | {title, price: (.price|tonumber), url}","mode": "perItem","outputFile": "csv"}
Each run of the scraper hands its dataset over; the cleaned items appear in this Actor's own dataset, and the CSV is in its key-value store. In per-item mode the dataset is read 1,000 items at a time, so a dataset with hundreds of thousands of items does not have to fit in memory.
Run it on a schedule with only new items
Turn on onlyNew and schedule the run. The Actor keeps a list of the keys it has already output in a key-value store named jq-transform-seen in your account. keyPath is a jq expression for the key of one output item (.url by default; .id, or (.shop + "/" + .sku) work too). On the next run, results whose key is in the list are skipped and not charged.
{"datasetId": "{{resource.defaultDatasetId}}","program": "{id, title, price: (.price|tonumber)}","onlyNew": true,"keyPath": ".id"}
- The list belongs to one program plus key expression. If you change either, you start with an empty list (so two different jobs in one account never hide each other's items).
- A result without a key (the expression gives
nullor fails) is always kept and never remembered. - Keys are only marked as seen after their item is written to the dataset. Results cut off by a charge limit or a time limit are picked up by the next run.
- Duplicate keys inside one run are also skipped (the first one wins).
- The list holds up to 300,000 keys (the oldest are dropped). If the store cannot be opened, the run stops before charging anything.
Input
| Field | Default | What it does |
|---|---|---|
program | the example above | the jq program |
mode | perItem | perItem runs the program on each item (0, 1 or many results per item, streams through big datasets). wholeArray gives the program all items as one array (for group_by, unique_by, sort_by, totals); capped by maxItems |
json | 4 example products | inline items (a JSON array) |
datasetId | - | read items from this dataset |
sourceUrl | - | a public link to a JSON array/object or a JSON-lines file (up to 200 MB). Private and local addresses are blocked |
onlyNew, keyPath | off, .url | skip results whose key was output in an earlier run |
outputFile | none | also store all results as one csv or json file in the run's key-value store |
maxItems | 100000 | most input items read in a run (1 to 1,000,000) |
If you give more than one source, they are read in the order json, datasetId, sourceUrl, up to maxItems items in total.
How results become items:
- Each jq output value is one dataset item. A result that is not an object (a string, a number, an array,
null) is stored as{"value": ...}. - In
wholeArraymode, a program that returns one array (likeunique_by(.url)orsort_by(.price)) gives one item per element. Use.[]yourself if you prefer. - Numbers keep their precision (jq 1.8 keeps big integers as written unless the program changes them).
Output files
With outputFile set to csv or json, the results are also stored in the run's key-value store as OUTPUT.csv or OUTPUT.json. The link is written to the log and to the OUTPUT record, together with the run summary:
{"itemsIn": 4,"itemsOut": 3,"errors": 0,"firstErrors": [],"skippedAlreadySeen": 0,"charged": { "item-out": 3 },"stopReason": null,"seconds": 0.3,"files": {}}
CSV columns are the union of all fields in order of first appearance. Nested values (objects and arrays) are written as JSON text in the cell, and cells with commas, quotes or line breaks are quoted. The results are held in memory while the file is built.
Errors you can read
-
A syntax error stops the run before anything is read or charged, and shows jq's own message, for example:
The jq program does not compile:error: syntax error, unexpected end of file, expecting '}' at <top-level>, line 1, column 33:{title, price: (.price|tonumber)^Nothing was charged. -
A runtime error in per-item mode (for example
tonumberon a text that is not a number) names the item and carries on with the others:jq error at item index 2 (counting from 0): string ("abc") cannot be parsed as a number - item: {"price":"abc"}. The first 20 are in theOUTPUTrecord, and the status says how many items failed. If every item fails, the run fails with the first message. -
A runtime error in whole-array mode fails the run (the program ran once, on everything).
-
A program that never ends is cut off after 60 seconds per batch of items (300 seconds in whole-array mode) with the item range it was working on.
Cookbook: 8 jq recipes for scraped data
Each recipe below is run by the Actor's test suite with the example shown.
1. Flatten nested fields
{id, name: .user.name, city: .user.address.city}
{"id": 1, "user": {"name": "Ana", "address": {"city": "Lisbon", "zip": "1000"}}} becomes {"id": 1, "name": "Ana", "city": "Lisbon"}.
2. Rename fields
{title: .name, url: .href, price: .cost}
{"name": "Lamp", "href": "https://x.test/lamp", "cost": 20, "junk": true} becomes {"title": "Lamp", "url": "https://x.test/lamp", "price": 20}. Fields you do not name are dropped.
3. Dedupe by a key (mode wholeArray)
unique_by(.url)
Three items where two share url: "b" give two items; the first of each key is kept (the result is sorted by the key).
4. Filter by date (mode perItem)
select(.date >= "2025-01-01")
ISO dates compare correctly as text, so this keeps 2025-01-01 and 2025-06-30T10:00:00Z and drops 2024-12-31. Items that do not match produce no output, which is how filtering works in jq.
5. Split an array into rows
{id, image: .images[]}
{"id": 1, "images": ["a.jpg", "b.jpg"]} becomes two items, {"id": 1, "image": "a.jpg"} and {"id": 1, "image": "b.jpg"}. An item with an empty list gives no rows.
6. Pick fields
pick(.title, .price)
{"title": "T", "price": 5, "html": "<p>x</p>", "debug": {}} becomes {"title": "T", "price": 5}. To drop a few fields and keep the rest, use del(.html, .debug).
7. String to number
.price |= (gsub("[^0-9.]"; "") | tonumber)
{"price": "$1,299.50"} becomes {"price": 1299.5}. Add select(.price != null) | in front to skip items without a price, because tonumber on null is an error.
8. Group and count (mode wholeArray)
group_by(.category) | map({category: .[0].category, count: length})
Items with category shoes, socks, shoes, shoes give {"category": "shoes", "count": 3} and {"category": "socks", "count": 1}.
Speed and size
Measured on our own machine (Windows, Node 24, not on the Apify platform), with 100,000 synthetic product items of about 350 bytes each (nested seller object, a tags list, 10% without a price):
| Run | Time | Peak memory |
|---|---|---|
per item: filter, tonumber, rename, join tags (100,000 in, 90,000 out) | 3.1 s | 108 MB |
| the same with a CSV file built at the end | 2.8 s | 262 MB |
| the same with "Only new items" on | 3.1 s | 287 MB |
whole array: group_by(.category) and count | 3.0 s | 502 MB |
whole array: unique_by(.seller.name) | 3.4 s | 534 MB |
These times are for the jq work with the results counted but not stored. The complete Actor run on the same data (reading a local dataset of 100,000 items, writing 90,000 result items) took 423 seconds and peaked at 353 MB, almost all of it Apify's local test storage writing one file per item; on the platform the dataset writes are batched, and we have not timed that part there. Whole-array mode needs about 4 to 5 MB of memory per 1,000 of these items on top of the base, so the default 1,024 MB covers roughly 100,000 items of this size; raise the Actor's memory for larger items.
Limits
- The jq program runs inside this Actor only; it cannot read environment variables, files or the network (
import,includeand$ENVsee nothing of the host). - Per-item mode streams a dataset, but the output file (
outputFile) and whole-array mode hold their data in memory. At the default 1,024 MB, keep those to about 100,000 small items; raise the Actor's memory for more. - A link source is read into memory (up to 200 MB). Only public http(s) links are allowed.
import/includeof jq modules and--argstyle variables are not available; put values in the program text.
日本語
Apify のデータセット(またはインライン JSON、JSON ファイルへのリンク)に対して、本物の jq(WebAssembly 版 jq 1.8)のプログラムを実行し、結果をデータセットの項目として返します。CSV / JSON ファイルの同時出力にも対応。スクレイパーの後にインテグレーションとして実行する使い方を想定しています(datasetId に {{resource.defaultDatasetId}})。項目ごとの実行(大きなデータセットも少しずつ処理)と、配列全体を渡す実行(group_by・unique_by など)を選べます。「新しい項目のみ」を有効にすると、過去の実行で出力済みのキーを飛ばします。jq の構文エラーは課金前に止まり、実行時エラーは項目の番号付きで表示されます。料金は出力1件あたり $0.0001(1,000件で $0.10)、書き込めた分だけの課金です。