Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong result #2

Open
amitguptagwl opened this issue Jan 22, 2017 · 9 comments
Open

Wrong result #2

amitguptagwl opened this issue Jan 22, 2017 · 9 comments

Comments

@amitguptagwl
Copy link
Contributor

amitguptagwl commented Jan 22, 2017

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 amitguptagwl changed the title Correct result Wrong result Jan 22, 2017
@f5io
Copy link
Owner

f5io commented Jan 22, 2017

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.

@amitguptagwl
Copy link
Contributor Author

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
Copy link
Contributor Author

amitguptagwl commented Jan 23, 2017

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

@f5io
Copy link
Owner

f5io commented Jan 23, 2017

@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?

@amitguptagwl
Copy link
Contributor Author

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
Copy link
Contributor Author

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

@f5io
Copy link
Owner

f5io commented Jan 23, 2017

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

@amitguptagwl
Copy link
Contributor Author

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

f5io added a commit that referenced this issue Jan 28, 2017
@f5io
Copy link
Owner

f5io commented Jan 30, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants