json icon indicating copy to clipboard operation
json copied to clipboard

Merge of two files no longer works

Open sinewave440hz opened this issue 7 years ago • 6 comments

Doing this, as in the docs: cat test.json test2.json | json --merge gives the error: json: error: input is not JSON: Syntax error at line 24, column 1: { Previously it didn't, of course.

sinewave440hz avatar Mar 17 '17 17:03 sinewave440hz

Repro:

$ cat b.json a.json b.json
{"foo":2}{
    "foo": 1
}
{"foo":2}$
$ cat b.json a.json b.json | ./lib/json.js --merge
json: error: input is not JSON: Syntax error at line 1, column 10:
        {"foo":2}{
        .........^
{"foo":2}{
    "foo": 1
}
{"foo":2}

$ ./lib/json.js --version
json 9.0.6

@sinewave440hz However, I see this all the way back to json v4 when --merge was introduced:

$ json4 --version
(node) util.puts is deprecated. Use console.log instead.
json 4.0.1
[21:24:21 trentm@danger0:~/tm/json (master)]
$ cat b.json a.json b.json | json4 --merge
json: error: input is not JSON: Syntax error at line 1, column 10:
        {"foo":2}{
        .........^
{"foo":2}{
    "foo": 1
}
{"foo":2}

Can you show me a case (showing the specific test.json and test2.json content) where that works in an earlier version of json and fails on newer versions? Thanks.

trentm avatar Mar 18 '17 04:03 trentm

Apparently not, I tried on earlier versions last night and got the same result. I haven't dug much further into it, but I had a script working earlier that combined two package.jsons successfully. This was on a much older node.js though - 4.4.7. I will try and put aside some time to take a look at this, but in the meantime, how can we do the equivalent of the merge example in your docs, but with files? Any suggestions?

sinewave440hz avatar Mar 18 '17 10:03 sinewave440hz

My bet is that the difference is that your files don't end with a newline. I think jsons current attempts to handle adjacent JS objects with --merge and -g have limitations when there are two incomplete objects on a given line.

trentm avatar Mar 20 '17 16:03 trentm

Merge error work around

  • Fails:

$ json -f dump.json -f dump_new.json --merge >dump_merge.json
json: error: input is not JSON: Syntax error at line 20427, column 2:
        }{
        .^

  • works:

$ json -f dump.json  >dump_1.json
$ json  -f dump_new.json >dump_2.json
$ json --merge -f dump_1.json -f dump_2.json  >dump_merge.json

eddyparkinson avatar Jul 19 '17 08:07 eddyparkinson

@eddyparkinson , didn't understand your example

@trentm has said right thing, your files with json must have empty line in the end

kondaurovDev avatar Sep 24 '18 16:09 kondaurovDev

json is not good with newlines. It is very picky when merging. But if you first do

json -f filename.json >tmpfile.json

then it fixes the newlines. tmpfile.json will have the needed newlines and can be used with json --merge

eddyparkinson avatar Sep 25 '18 00:09 eddyparkinson