gdritter repos lib-static / master bin / json-list
master

Tree @master (Download .tar.gz)

json-list @masterraw · history · blame

#!/usr/bin/python2

import json
import sys
import yaml

def coerce(arg):
  if not arg: return None
  if arg[0] == '@':
    try:
      with open(arg[1:]) as f:
        return coerce(f.read())
    except: pass
  if arg == '-': return coerce(sys.stdin.read())
  try:    return yaml.safe_load(arg)
  except: pass
  try:    return json.loads(arg)
  except: pass
  return arg

if sys.argv[1:] and (sys.argv[1] == '-c' or sys.argv[1] == '--cat-files'):
  l = []
  for n in sys.argv[2:]:
    with open(n) as f:
      l.append(coerce(f.read()))
  sys.stdout.write(json.dumps(l))
else:
  sys.stdout.write(json.dumps(map(coerce, sys.argv[1:])))
sys.stdout.write("\n")