Skip to content

Instantly share code, notes, and snippets.

@toniher
Created March 8, 2023 16:23
Show Gist options
  • Save toniher/6bfc8f517ccd7278144ac3534d79a694 to your computer and use it in GitHub Desktop.
Save toniher/6bfc8f517ccd7278144ac3534d79a694 to your computer and use it in GitHub Desktop.
Fast processing of a CouchDB json import file. Related to this: https://simeon-yan-iliev.medium.com/export-import-a-database-with-couchdb-74bfec3831bc
#!/usr/bin/env python
import sys
import json
if len(sys.argv) != 2:
exit()
print("{\"docs\": [")
with open(sys.argv[1]) as infile:
next(infile)
for line in infile:
line = line.rstrip()
if line.startswith("{\"id"):
doc = json.loads(line.rstrip(","))
# Instead of specific values, if all doc, we could simply remove _rev below
struct = {"_id": doc["doc"]["_id"], "off": doc["doc"]["off"]}
end = ""
if line.endswith(","):
end = ","
print(json.dumps(struct) + end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment