Skip to content

Instantly share code, notes, and snippets.

@mikesname
Last active May 5, 2020 19:38
Show Gist options
  • Save mikesname/fca11a2d876b16b0f056aa8a4a8d5e5d to your computer and use it in GitHub Desktop.
Save mikesname/fca11a2d876b16b0f056aa8a4a8d5e5d to your computer and use it in GitHub Desktop.
Processing data from GraphQL to CSV. Install the ijson module first.
#!/usr/bin/env python3
import csv, ijson, sys
items = ijson.items(sys.stdin, "data.documentaryUnits.items.item")
csvout = csv.writer(sys.stdout, quoting = csv.QUOTE_MINIMAL)
for item in items:
for description in item["descriptions"]:
row = [
description["scopeAndContent"],
description["biographicalHistory"],
description["systemOfArrangement"]]
if [r for r in row if r is not None]:
csvout.writerow([item["id"]] + row)
query documentaryUnitData {
documentaryUnits {
items {
id
descriptions {
languageCode
biographicalHistory
scopeAndContent
systemOfArrangement
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment