0

I have

{"a":1} {"b":2}

and I want to use jq to get

{"a": 1, "b":2}

jq --slurp add myfile works great. However, I want to use it with jq.py, which doesn't have slurp mode. Does jq have a way to do this for arbitrary sequences of objects (with distinct keys) without using --slurp?

jq170727
  • 13,159
  • 3
  • 46
  • 56
Hatshepsut
  • 5,962
  • 8
  • 44
  • 80

3 Answers3

2

Less demanding on memory than using [inputs] would be:

reduce inputs as $in ({}; . + $in)

The above assumes the -n command-line option is in effect. If this is not possible with py.jq, then the following filter should be used instead:

reduce inputs as $in (.; . + $in)

p.s. Perhaps the author of py.jq will be willing to address the issue concerning command-line options if made aware of it?

peak
  • 105,803
  • 17
  • 152
  • 177
1

If you're reading myfile with python, the splitstream module described here may be what you want. Here is a test example (test.py) which uses jq.py.

import splitstream
from jq import jq

def slurp(filename):
    with open(filename) as f:
        for s in splitstream.splitfile(f, format="json"):
            yield s

obj = {}
for jstr in slurp('myfile'):
    obj = jq("[%s, .] | add" % obj).transform(text=jstr, text_output=True)

print obj

Here is a sample run

$ cat myfile
{"a":1}
{"b":2}

$ python test.py
{"a":1,"b":2}

Although this appears to do what you asked for using jq.py I don't think its a good solution because sharing the state between python and jq is clumsy and inefficient.

A better approach might be to use jq as a subprocess. Here is an example (test2.py):

import json
import sh

cmd = sh.jq('-M', '-s', 'add', 'myfile')
obj = json.loads( cmd.stdout )
print json.dumps(obj, indent=2)

Sample run:

$ python test2.py
{
  "a": 1, 
  "b": 2
}
jq170727
  • 13,159
  • 3
  • 46
  • 56
1

This produces the desired output.

jq -nc '[inputs] | add'

I can't say if it would work with jq.py, however.

user197693
  • 1,935
  • 10
  • 8