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

Using jsonpath filtering on arrays breaks sub-templates #9

Open
dremekie opened this issue May 27, 2016 · 2 comments
Open

Using jsonpath filtering on arrays breaks sub-templates #9

dremekie opened this issue May 27, 2016 · 2 comments

Comments

@dremekie
Copy link

Hi. Thanks for this helpful library.

Please see the following 3 code snippets. The second version fails. I am trying to apply a sub-template transformation to a filtered (via jsonpath notation) array and it fails. Without the filter, it works.

var transform = require('jsonpath-object-transform');

var o = {"cities": [
    {"city": "Toronto", "population": 2.5},
    {"city": "New York", "population":8.1},
    {"city": "San Diego", "population": 1.6},
    {"city": "Seattle", "population": 0.65}
]};

// works
var template = {
    bigCities: ["$.cities[?(@.population > 2)]"]
};

var r = transform(o, template);
console.log(JSON.stringify(r, null, 4));

Results in:

{
    "bigCities": [
        {
            "city": "Toronto",
            "population": 2.5
        },
        {
            "city": "New York",
            "population": 8.1
        }
    ]
}
var transform = require('jsonpath-object-transform');

var o = {"cities": [
    {"city": "Toronto", "population": 2.5},
    {"city": "New York", "population":8.1},
    {"city": "San Diego", "population": 1.6},
    {"city": "Seattle", "population": 0.65}
]};

// fails
var template = {
    bigCities: ["$.cities[?(@.population > 2)]", {"name": "city"}]
};

var r = transform(o, template);
console.log(JSON.stringify(r, null, 4));

results in

TypeError: seek[0].forEach is not a function
    at seekArray (/Users/dwayne/tmp/test/node_modules/jsonpath-object-transform/lib/jsonpath-object-transform.js:96:15)
    at walk (/Users/dwayne/tmp/test/node_modules/jsonpath-object-transform/lib/jsonpath-object-transform.js:52:7)
    at /Users/dwayne/tmp/test/node_modules/jsonpath-object-transform/lib/jsonpath-object-transform.js:118:7
    at Array.forEach (native)
    at seekObject (/Users/dwayne/tmp/test/node_modules/jsonpath-object-transform/lib/jsonpath-object-transform.js:117:26)
    at walk (/Users/dwayne/tmp/test/node_modules/jsonpath-object-transform/lib/jsonpath-object-transform.js:52:7)
    at /Users/dwayne/tmp/test/node_modules/jsonpath-object-transform/lib/jsonpath-object-transform.js:131:5
    at Object.<anonymous> (/Users/dwayne/tmp/test/json-transform.js:17:9)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
var transform = require('jsonpath-object-transform');

var o = {"cities": [
    {"city": "Toronto", "population": 2.5},
    {"city": "New York", "population":8.1},
    {"city": "San Diego", "population": 1.6},
    {"city": "Seattle", "population": 0.65}
]};

// works
var template = {
    bigCities: ["$.cities", {"name": "city"}]
};

var r = transform(o, template);
console.log(JSON.stringify(r, null, 4));

results in

{
    "bigCities": [
        {
            "name": "Toronto"
        },
        {
            "name": "New York"
        },
        {
            "name": "San Diego"
        },
        {
            "name": "Seattle"
        }
    ]
}
@dremekie dremekie changed the title Using jsonpath filtering Using jsonpath filtering on arrays breaks sub-templates May 28, 2016
@ferronrsmith
Copy link

ferronrsmith commented Jun 20, 2016

this issue can be resolved by auto-wrapping the seek result if the length is > 1 and a subpath exists.

conscia@e93f105

@Rendiere
Copy link

@ferronrsmith can you show how you would have done this? I have the same problem, but struggling with the exact syntax.

Thanks a lot man

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

3 participants