jsonpath icon indicating copy to clipboard operation
jsonpath copied to clipboard

Wrong result

Open amitguptagwl opened this issue 8 years ago • 9 comments

Hi,

Your project may be in very early stage. But I thought to add to comparison of various jsonpath js libraries. But I seen that it gave wrong output in case of first query.

I just wanted to to inform you.

var jp = require('@f5io/jsonpath');

var obj = {
    'a': {
        1: 'la',
        2: 'boo',
        'h': [
            {foo: [1,2,3]},
            {foo: [4,5,6]},
            {foo: 12, name: 'a'},
            {foo: 13.5, name: { 'h': 45}},
            {foo: 11.8, name: 'c'},
            true,
            123,
            [3,4,5]
        ]
    },
    'b': {
        1: 'la',
        2: 'boo',
        'h': [
            {foo: [1,2,3]},
            {foo: [4,5,6]},
            {foo: 12, name: 'a'},
            {foo: 13.5, name: { 'h': 45}},
            {foo: 11.8, name: 'c'},
            true,
            123,
            [3,4,5]
        ]
    },
    'c': {
        1: 'la',
        2: 'boo',
        'h': [
            {foo: [1,2,3]},
            {foo: [4,5,6]},
            {foo: 12, name: 'a'},
            {foo: 13.5, name: { 'h': 45}},
            {foo: 11.8, name: 'c'},
            true,
            123,
            [3,4,5]
        ]
    },
    'd': {
        1: 'la',
        2: 'boo',
        'h': [
            {foo: [1,2,3]},
            {foo: [4,5,6]},
            {foo: 12, name: 'a'},
            {foo: 13.5, name: { 'h': 45}},
            {foo: 11.8, name: 'c'},
            true,
            123,
            [3,4,5]
        ]
    }
}

var patterns = {
    pattern1: '$..h[*].foo',
    pattern2: '$.b[1,2]',
    pattern3: '$.b.2',
    pattern4: '$..h[?(@.foo>13)]'
}

console.log(jp(patterns.pattern1,obj)); //Incorrect
console.log(jp(patterns.pattern2,obj)); //correct
console.log(jp(patterns.pattern3,obj)); //correct
console.log(jp(patterns.pattern4,obj)); //correct

amitguptagwl avatar Jan 22 '17 18:01 amitguptagwl

Hi @amit, thanks for taking the time to look into doing a comparison.

To be perfectly honest, this implementation is pretty loose to the JSONPath specification, so I wouldn't expect a lot of queries to give an exact match to other libraries, however, I am always looking to improve it.

Could you provide me with an actual response and an expected response, obviously I could do this myself, but it would be good to document it here too.

Again, thanks, I will make sure to look into it.

f5io avatar Jan 22 '17 19:01 f5io

For the first query,

Expected

[ [ 1, 2, 3 ],
  [ 4, 5, 6 ],
  12,
  13.5,
  11.8,
  [ 1, 2, 3 ],
  [ 4, 5, 6 ],
  12,
  13.5,
  11.8,
  [ 1, 2, 3 ],
  [ 4, 5, 6 ],
  12,
  13.5,
  11.8,
  [ 1, 2, 3 ],
  [ 4, 5, 6 ],
  12,
  13.5,
  11.8 ]

Actual

[ [ 1, 2, 3 ],
  [ 4, 5, 6 ],
  12,
  13.5,
  11.8,
  undefined,
  undefined,
  undefined,
  undefined,
  [ 1, 2, 3 ],
  [ 4, 5, 6 ],
  12,
  13.5,
  11.8,
  undefined,
  undefined,
  undefined,
  undefined,
  [ 1, 2, 3 ],
  [ 4, 5, 6 ],
  12,
  13.5,
  11.8,
  undefined,
  undefined,
  undefined,
  undefined,
  [ 1, 2, 3 ],
  [ 4, 5, 6 ],
  12,
  13.5,
  11.8,
  undefined,
  undefined,
  undefined,
  undefined ]

However not completely wrong ;)

From performance wise it is best among all 5 jsonpath implementation. So I'll suggest if you can complete and test it against all the scenarios.

I'm excited to put it on changejs.

amitguptagwl avatar Jan 22 '17 21:01 amitguptagwl

Ah it failed one more test. So I'm removing it from comparison table. But hoping best for it's progress.

amitguptagwl avatar Jan 23 '17 07:01 amitguptagwl

@amitguptagwl: I have a fix ready to go for the previous incorrect response. Is there anywhere I can see the other test specs which you are trying?

f5io avatar Jan 23 '17 13:01 f5io

Try this code


var arraySize = 2333,
    resultCount = 150,
    itemCount = 50,
    groupCount = 145;

var json = {
    results: []
};

var i, j;

var bigArray = [];
for (i = 0; i < arraySize; i++) {
    bigArray[i] = 1;
}

var items = [];
for (i = 0; i < itemCount; i++) {
    items[i] = JSON.parse(JSON.stringify({a: {b: 0, c: 0}, s: {b: {c: bigArray}}}));
}

for (i = 0; i < resultCount; i++) {
    json.results[i] = {groups: [], v: {v: [1, 2, 3, 4, 5, 6, 7, 8]}};
    json.results[i].groups = [];
    for (j = 0; j < groupCount; j++) {
        json.results[i].groups[j] = {items: items, a: "121212"};
    }
}

var jp = require('@f5io/jsonpath');
console.log(jp('$.results[*].groups[*].items[42]',json));

amitguptagwl avatar Jan 23 '17 15:01 amitguptagwl

Seems I have become a tester (have secured my future) ;)

amitguptagwl avatar Jan 23 '17 15:01 amitguptagwl

thanks, will have a look at this hopefully this evening 👍

f5io avatar Jan 23 '17 15:01 f5io

Plz give a star, if it helped you a bit work

amitguptagwl avatar Jan 27 '17 21:01 amitguptagwl

Just so you know, I am looking at resolving the other issue. I am not sure it is my misunderstanding of the, admittedly loose, spec or the other implementations misinterpretation. I will continue looking

f5io avatar Jan 30 '17 12:01 f5io