bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.56k stars 329 forks source link

Why is not filtering? #1022

Open hugomf opened 4 years ago

hugomf commented 4 years ago

Im trying to filter out a collection based on a value that is given in the parent, is it possible? if I hardcoded the value as FILTER is working, but if I'm setting the value from the parent attribute, it is not filtering

Input:

{
    "parent" : {
        "field1" : "value1",
        "field2" : "value2",
        "filterfield" : "FILTER",
        "children" : [{
            "id" : "FILTER",
            "value" : "child1"
        },{
            "id" : "OTHER",
            "value" : "child2"
            }
        ]
    }
}

Spec:

[
    {
        "operation": "shift",
        "spec": {
            "parent": {
                "children": {
                    "*": {
                        "@(0,id)" : {
                            "@(3,filterfield)" : { //If I pass "FILTER" is working
                                "@2"  : {
                                    "value" : "test"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
]

expected output:

{
    "children" : {
        "id" : "FILTER",
        "value" : "child1"
    }
}
hugomf commented 4 years ago

I am new using Jolt, I think this tool is very helpful, I was misunderstanding the concept, I fixed it but is there any other simpler way?

   {
        "operation": "shift",
        "spec": {
            "parent": {
                "filterfield": {
                    "*": { // saving the values ..
                        "@2": { // puts the pointer again in the root
                            "children": {
                                "*": {
                                    "id": {
                                        "&4": { // matching filterfield with the current child id 
                                            // points back to the root of the json tree
                                            // and fetchs the matching children node.
                                            "@2": "children"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }