yudai / gojsondiff

Go JSON Diff
Other
536 stars 79 forks source link

Failed to unmarshal valid json #15

Open kuznet1 opened 7 years ago

kuznet1 commented 7 years ago
$ echo [1] > 1.json
$ echo [2] > 2.json
$ jd 1.json 2.json

Failed to unmarshal file: json: cannot unmarshal array into Go value of type map[string]interface {} exit status 3

yudai commented 7 years ago

Thanks for the report. I'll take a look into the issue.

blaskovicz commented 7 years ago

It looks like it's because the Compare() function assumes that the outer layer of the JSON is an object, not an array. The function.

As an easy workaround for now, you can always wrap the actual object in another object. Example: {"_": expectedData } vs {"_": actualData }.

yudai commented 7 years ago

Working on the fix_15 branch.

kidandcat commented 4 years ago

it cannot properly unmarshall this too: {["this is valid json"]}

cold-bin commented 5 months ago

maybe, this way is help to you:

func AnyJsonDiff(left interface{}, right interface{}) (gojsondiff.Diff, error) {
    type Container struct {
        Payload any
    }

    leftBs, err := json.Marshal(Container{left})
    if err != nil {
        return nil, err
    }
    rightBs, err := json.Marshal(Container{right})
    if err != nil {
        return nil, err
    }

    return gojsondiff.New().Compare(leftBs, rightBs)
}