File: jsonl.py
   1 #!/usr/bin/python
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright (c) 2026 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the "Software"), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 from json import load, dump
  27 from sys import argv, exit, stderr, stdin, stdout
  28 
  29 
  30 info = '''
  31 jsonl [options...] [filepath/URI...]
  32 
  33 JSON Lines turns valid JSON-input arrays into separate JSON lines, one for
  34 each top-level item. Non-arrays result in a single JSON-line.
  35 
  36 When not given a filepath or URI to load, standard input is used instead.
  37 Every output line is always a single top-level item from the input.
  38 
  39 All (optional) leading options start with either single or double-dash:
  40 
  41     -h, -help    show this help message
  42 '''
  43 
  44 # handle standard help cmd-line options, quitting right away in that case
  45 if len(argv) > 1 and argv[1] in ('-h', '--h', '-help', '--help'):
  46     print(info.strip())
  47     exit(0)
  48 
  49 
  50 def jsonl(w, src):
  51     data = load(src)
  52     if isinstance(data, (list, tuple)):
  53         for v in data:
  54             emit(w, v)
  55     else:
  56         emit(w, data)
  57 
  58 
  59 def emit(w, v):
  60     dump(v, w, indent=None, allow_nan=False, separators=(', ', ': '))
  61     w.write('\n')
  62 
  63 
  64 def seems_url(s):
  65     protocols = ('https://', 'http://', 'file://', 'ftp://', 'data:')
  66     return any(s.startswith(p) for p in protocols)
  67 
  68 
  69 args = argv[1:]
  70 
  71 if len(args) > 0:
  72     s = args[0]
  73     if s != '-' and s != '--' and s.startswith('-'):
  74         print(f'jsonl: unsupported option {s}')
  75         exit(1)
  76 
  77 if len(args) > 0 and args[0] == '--':
  78     args = args[1:]
  79 
  80 try:
  81     if len(args) > 1:
  82         raise ValueError('jsonl: multiple inputs not allowed')
  83     name = args[0] if len(args) > 0 else '-'
  84 
  85     if name == '-':
  86         jsonl(stdout, stdin.buffer)
  87     elif seems_url(name):
  88         from urllib.request import urlopen
  89         with urlopen(name) as inp:
  90             jsonl(stdout, inp)
  91     else:
  92         with open(name, mode='rb') as inp:
  93             jsonl(stdout, inp)
  94 except BrokenPipeError:
  95     # quit quietly, instead of showing a confusing error message
  96     stderr.close()
  97     exit(0)
  98 except KeyboardInterrupt:
  99     exit(2)
 100 except Exception as e:
 101     print(str(e), file=stderr)
 102     exit(1)