# Query GTM API prices with DuckDB, jq and Python

Example queries on datacircle.json with DuckDB, jq and Python. Find an API's cheapest paid plans, the best scored plans, or one provider's plans.

Every query below runs on `datacircle.json` as you download it. The DuckDB queries also run on `datacircle.csv`. jq and Python read the JSON only.

## DuckDB

An API's paid plans under $50, cheapest per 1,000 first:

```sql
SELECT provider, plan, billing, pay, currency, perThousand, score, page, docs
FROM 'datacircle.json'
WHERE api = 'Employee email' AND billing != 'free' AND pay * exchangeRate <= 50 AND perThousand IS NOT NULL
ORDER BY perThousand LIMIT 10;
```

An API's best scored plans, with each part of the score:

```sql
SELECT provider, plan, call, score, priceScore, dataScore, costToGetStartedScore, bundleScore, performanceScore, page, docs
FROM 'datacircle.json'
WHERE api = 'Employee profile' AND score IS NOT NULL
ORDER BY score DESC LIMIT 10;
```

One provider's plans:

```sql
SELECT api, plan, billing, pay, perThousand, page, docs
FROM 'datacircle.json'
WHERE provider ILIKE '%fetchin%'
ORDER BY api, perThousand;
```

The cheapest paid plan per 1,000 of each API:

```sql
SELECT api, arg_min(provider || ', ' || plan, perThousand) AS plan, min(perThousand) AS perThousand
FROM 'datacircle.json'
WHERE billing != 'free' AND perThousand IS NOT NULL
GROUP BY api ORDER BY api;
```

The APIs under one category:

```sql
SELECT subcategory, sheet, api, count(*) AS plans
FROM 'datacircle.json'
WHERE category = 'Email and phone'
GROUP BY ALL ORDER BY subcategory, sheet;
```

## jq

```bash
jq '[.[] | select(.api == "Employee email" and .billing != "free" and .pay != null and .pay * .exchangeRate <= 50 and .perThousand != null)]
  | sort_by(.perThousand) | .[:10] | map({provider, plan, pay, currency, perThousand, score, page, docs})' datacircle.json
```

## Python

```python
import json

rows = json.load(open("datacircle.json"))
found = sorted(
    (r for r in rows if r["api"] == "Employee email" and r["billing"] != "free" and r["pay"] is not None and r["pay"] * r["exchangeRate"] <= 50 and r["perThousand"] is not None),
    key=lambda r: r["perThousand"],
)[:10]
for r in found:
    print(r["provider"], r["plan"], r["perThousand"], r["page"], r["docs"])
```
