catalogue / tools / Documents / JSON to CSV API

JSON to CSV API

Renders JSON objects as raw CSV with no spreadsheet formula protection; columns are inferred or given, nested values are JSON-encoded.

Endpoint
POST /v1/json-to-csv
Price
$0.005 per call
Family
Documents

01When to use it

  • Correct CSV output is quoting and escaping to RFC-4180, not a join on commas, and the column set has to be reconciled across rows with different keys, fiddly enough that paying for it beats hand-rolling it per call. Headers and string values preserve formula prefixes (=, +, -, @), tabs and carriage returns without added apostrophes or trimming. CSV quoting escapes delimiters, quotes and newlines but does not neutralize spreadsheet formulas. Use spreadsheet-specific import controls for untrusted data. CRLF separates records.
  • Your source is JSON and you need CSV.

When not to use it

  • The input is over 25 MB (26,214,400 bytes): json-to-csv answers too_large (413).
  • The destination needs more than 30 s to answer: the call ends with upstream_timeout (424).
  • The destination is on a private or local network: the network guard refuses it with blocked_target (403).

02Parameters

FieldTypeRequiredDescription
urlstringyesAbsolute http(s) URL of a UTF-8 JSON document (a leading BOM is allowed) that is an array of objects or a single object, up to 2048 characters; redirects are followed.
columnsarraynoSelects columns in exactly the supplied order; a missing key produces an empty field, except an absent __proto__ column renders {}. When omitted, visits only rows retained by max_rows, in input order, appending each unseen key once. Within each row, JavaScript enumerates canonical array-index keys (0 through 4294967294) numerically ascending, then other string keys in insertion order. Names such as 01, -1 and 4294967295 are ordinary string keys. New keys from later rows are appended, not globally sorted; textual JSON key order is not preserved for array-index keys.
max_rowsintegernoMaximum rows rendered, 1 to 100000, default 10000; later rows are dropped and truncated is set, and inferred columns come only from the kept rows. Default: 10000.

03Limits

LimitValue
Largest source file25 MB (26,214,400 bytes)
Timeout30 s
Source formatsJSON
Output formatsCSV

04Example

The published example of json-to-csv, verbatim: the request body and the response it returns.

Request
POST /v1/json-to-csv
content-type: application/json
payment-signature: <base64 x402 payload>

{
  "url": "https://grist.tools/samples/documents/garden-harvest.json"
}
Response
200 OK
payment-response: <base64 settlement receipt>

{
  "source_url": "https://grist.tools/samples/documents/garden-harvest.json",
  "columns": [
    "week",
    "crop",
    "kg",
    "plots",
    "note"
  ],
  "csv": "week,crop,kg,plots,note\r\n1,radish,2.5,\"[\"\"A1\"\",\"\"B2\"\"]\",\r\n1,lettuce,4,,\"first cut, outer leaves only\"\r\n2,peas,3.25,\"[\"\"A2\"\"]\",",
  "row_count": 3,
  "truncated": false
}

Sample file: documents/garden-harvest.json. JSON array of three harvest records whose keys differ from row to row, with a nested array value, a null and a string holding a comma.

Response fieldTypeIn the example
source_urlstring"https://grist.tools/samples/documents/garden-harvest.json"
columnsarray5 items
csvstring"week,crop,kg,plots,note 1,radish,2.5,"[""A1"",""B2""]", 1,lettuce,4,,"first cut, outer leaves only" 2,peas,3.25,"[""A2""]","
row_countnumber3
truncatedbooleanfalse

05Call it from code

JavaScript
// json-to-csv: $0.005 per call, paid in USD Coin on eip155:8453 via x402
// Install: npm install @x402/fetch@2 @x402/evm@2 viem@2
// Save as client.mjs (ES module, Node 18+), export EVM_PRIVATE_KEY with the paying wallet's key in your shell, then run: node client.mjs
// The wrapper reads the 402 (payment-required), signs and retries with payment-signature.
import { wrapFetchWithPayment, x402Client, decodePaymentResponseHeader } from '@x402/fetch'
import { ExactEvmScheme } from '@x402/evm/exact/client'
import { privateKeyToAccount } from 'viem/accounts'

const account = privateKeyToAccount(process.env.EVM_PRIVATE_KEY)
const client = new x402Client().register('eip155:8453', new ExactEvmScheme(account))
const fetchWithPayment = wrapFetchWithPayment(fetch, client)

const body = {
  "url": "https://grist.tools/samples/documents/garden-harvest.json"
}

const res = await fetchWithPayment('https://grist.tools/v1/json-to-csv', {
  method: 'POST',
  headers: { 'content-type': 'application/json' },
  body: JSON.stringify(body),
})
console.log(res.status, await res.json())
const settle = res.headers.get('payment-response')
if (settle) console.log(decodePaymentResponseHeader(settle).transaction)
Python
# json-to-csv: $0.005 per call, paid in USD Coin on eip155:8453 via x402
# Install: pip install "x402[requests,evm]>=2.24,<3"
# The session reads the 402 (payment-required), signs and retries with payment-signature.
import os
from eth_account import Account
from x402 import x402ClientSync
from x402.http import decode_payment_response_header
from x402.http.clients.requests import x402_requests
from x402.mechanisms.evm.exact import register_exact_evm_client
from x402.mechanisms.evm.signers import EthAccountSigner

account = Account.from_key(os.environ["EVM_PRIVATE_KEY"])
client = x402ClientSync()
register_exact_evm_client(client, EthAccountSigner(account), networks="eip155:8453")
session = x402_requests(client)

payload = {
    "url": "https://grist.tools/samples/documents/garden-harvest.json"
}

res = session.post("https://grist.tools/v1/json-to-csv", json=payload)
print(res.status_code, res.json())
settle = res.headers.get("payment-response")
if settle:
    print(decode_payment_response_header(settle).transaction)
curl
# json-to-csv: $0.005 per call, paid in USD Coin on eip155:8453 via x402
# 1. Unpaid call: HTTP 402, the requirements in the payment-required header (base64 JSON) and in the body.
curl -i -X POST 'https://grist.tools/v1/json-to-csv' -H 'content-type: application/json' -d '{"url":"https://grist.tools/samples/documents/garden-harvest.json"}'

# 2. Same call with the signed payment (an EIP-3009 authorization, EIP-712 signed: it cannot be
#    typed by hand). x-payment is accepted as the v1 alternative. HTTP 200 carries payment-response.
curl -i -X POST 'https://grist.tools/v1/json-to-csv' -H 'content-type: application/json' -H 'payment-signature: <base64 x402 payload>' -d '{"url":"https://grist.tools/samples/documents/garden-harvest.json"}'

06Errors

CodeHTTPFor this service
invalid_input400The body does not match the schema; its fields are url, columns and max_rows.
blocked_target403
unreachable_target424
upstream_timeout424
too_large413
unprocessable422
internal500

Input that is rejected before the tool runs (malformed JSON, schema mismatch, body over the size limit) is never charged.

When each code is raised, and what it means for payment: /docs/json-to-csv.

07Conversions

ConversionRecorded sampleSource sizeOutput size
JSON to CSVdocuments/garden-harvest.json233 bytes126 bytes

08Questions

How much does a json-to-csv call cost?
$0.005 per call, paid in USDC on Base via x402, with no account and no API key.
What does json-to-csv return?
A JSON object with 5 top-level fields: source_url, columns, csv, row_count and truncated. The example on this page is a real call over documents/garden-harvest.json.
What happens when a json-to-csv call fails?
The tool answers with a typed JSON error from the errors table. Payment is settled only after the tool has produced its result; a call that fails inside the tool is never settled. A 503 upstream_unavailable can follow a settled call; the x402 payments guide covers it, 429, 402 and an uncertain 500.

09Related