Skip to content

Instantly share code, notes, and snippets.

@alexpreynolds
Created January 6, 2017 22:14
Show Gist options
  • Save alexpreynolds/b4ef738d8838af5f770e40091a96548b to your computer and use it in GitHub Desktop.
Save alexpreynolds/b4ef738d8838af5f770e40091a96548b to your computer and use it in GitHub Desktop.
Convert JSON string to tab-delimited output
#!/usr/bin/env python
import sys
import json
test_json_str = '''
[
{"name":"FOO", "chr":"chrN", "start":100, "stop":200},
{"name":"BAR", "chr":"chrN", "start":300, "stop":400},
{"name":"BAZ", "chr":"chrN", "start":700, "stop":750}
]
'''.strip()
test_json_obj = json.loads(test_json_str)
for gene_obj in test_json_obj:
line = '\t'.join([gene_obj["chr"],
str(gene_obj["start"]),
str(gene_obj["stop"]),
gene_obj["name"]])
sys.stdout.write("%s\n" % line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment