-
Notifications
You must be signed in to change notification settings - Fork 54
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
How to transform complex objects in a list? #298
Comments
This will give you two arrays, you can join them later {
"items1": {
"#loop($.items[?(@.keyTypeA == 'One')])": {
"id": "some_id",
"keyTypeA": "some_key"
}
},
"items2": {
"#loop($.items[?(@.keyTypeA != 'One')])": "#currentvalue()"
}
} The only problem is getting elements that don't contain 'keyTypeA' property... |
I've notice that a function to negate an argument is missing, something like #not(<bool_value>). I managed to find an ugly workaround for this, though. Transformer {
"items": "#applyover({ 'items1': { '#loop($.items[?/(@.keyTypeA == `One`/)])': { 'id': 'some_id'/, 'keyTypeA': 'some_key' } }/, 'items2': { '#loop($.items[?/(@.keyTypeA != `One`/)])': '#currentvalue()' }/, 'items3': { '#loop($.items)': { '#ifgroup(#ifcondition(#existsandnotempty($.keyTypeA)/,False/,True/,False))': { '#': [ '#copy($)'] } } } }, '#xconcat(#valueof($.items1), #valueof($.items2), #valueof($.items3))')"
} Output {
"items": [{
"id": "some_id",
"keyTypeA": "some_key"
}, {
"id": "some_id",
"keyTypeA": "some_key"
}, {
"keyTypeA": "Two"
}, {
}, {
}, {
}, {
"keyTypeB": "Red"
}
]
} With #289, transformer would be easier to do and read, without all that escaped charecters {
"transform": [{
"items1": {
"#loop($.items[?(@.keyTypeA == `One`)])": {
"id": "some_id",
"keyTypeA": "some_key"
}
},
"items2": {
"#loop($.items[?(@.keyTypeA != `One`)])": "#currentvalue()"
},
"items3": {
"#loop($.items)": {
"#ifgroup(#ifcondition(#exists($.keyTypeA),False,True,False))": { //ugly workaround for a negate function
"#": ["#copy($)"]
}
}
}
}, {
"items": "#xconcat(#valueof($.items1), #valueof($.items2), #valueof($.items3))"
}
]
} Explanation:
|
Hey @Courela Thanks for the sample. Unfortunately I need to have the array without the null elements and ideally the order of the elements would be in the same order, I didn't try with your Courela:feature/multiple_transforms version, is that feature going to be added soon? |
I don't know, I'm just a contributor |
I've made some progress with the following transform
This gets me to
Now I just need to add or update the id. I thought that I should be able t Note: I need to be able to pass any elements of the item without the transform being aware of them, |
This works {
"items": {
"#loop($.items)": {
"#loop($)": {
"#eval(#currentproperty())": "#ifcondition(#currentproperty(),id,987234345,#ifcondition(#currentvalueatpath($.keyTypeA),One,Three,#currentvalueatpath(#concat($.,#currentproperty()))))"
}
}
}
} |
Hey @Courela, thanks that's closer to what I need, but that isn't quite right. The logic is if the element of the array has a property of "keyTypeA" with a value of "One".
There doesn't seam to be a away to append a property to an element. |
I’m trying to perform a transform on elected in an array. I need to update object in an array that has a matching key. If the key matches I need to change the key as well add an Id to the object.
Example payload
Transformed into
Any advice would be appreciated.
The text was updated successfully, but these errors were encountered: