Closed rodolfoag closed 7 years ago
I think
[
{
"operation":"shift",
"spec": {
"labels": "labels[0]"
},
"over": "metadata",
"inplace": true
}
]
ought to do the trick. The over
feature is underdocumented. Let me know if that does the job for you.
I tested the above solution and it seems to work:
See example (in.json
is your example input, out.json
is your example output, spec.json
is the spec I defined above):
➜ tmp git:(master) ✗ cat in.json | kazaam -spec spec.json | jq -cMS . > kazaam.out.json
➜ tmp git:(master) ✗ cat out.json | jq -cMS . > sorted.out.json
➜ tmp git:(master) ✗ diff -s kazaam.out.json sorted.out.json
Files kazaam.out.json and sorted.out.json are identical
Works like a charm @ryanleary! Thx mate.
Great! Happy to help!
As an aside, I'd love to hear how you're using kazaam and anything that might make it more useful to you. Thanks.
I'm using it for the exact same purpose you created it. I have some JSON files created by a Progress 4GL application (witch has very poor JSON support) and I'm transform those files to send to an API. Kazaam is helping a lot.
Well, I have another question: Can I use over within a child array? Like in the JSON below to transform the labels key inside every items.metadata:
{ "items": [ { "metadata": [ { "label": "Amount", "labels": [ { "en": "Amount", "ptBR": "Quantidade" } ] }, { "label": "Value", "labels": [ { "en": "Value", "ptBR": "Valor" } ] } ] }, { "metadata": [ { "label": "Amount", "labels": [ { "en": "Amount", "ptBR": "Quantidade" } ] }, { "label": "Value", "labels": [ { "en": "Value", "ptBR": "Valor" } ] } ] } ] }
Thanks!
I did it with the spec:
[
{
"operation": "shift",
"spec": {
"metadata[*].labels": "metadata[0].labels[0]"
},
"over": "items",
"inplace": true
}
]
Not sure if my spec is exactly right because all elements inside metadata where transformed, not only the 0 index. But it works!
Forget my last comment! For every element in items.metadata, kazaam replaced labels with its labels[0]. As it should be.
Hi, thx for the great work. I'm struggling to define a spec to transform the first element (labels) of an array in an object.
from:
{ "metadata": [ { "label": "Amount", "labels": [ { "en": "Amount", "ptBR": "Quantidade" } ] }, { "label": "Value", "labels": [ { "en": "Value", "ptBR": "Valor" } ] } ] }
into
{ "metadata": [ { "label": "Amount", "labels": { "en": "Amount", "ptBR": "Quantidade" } }, { "label": "Value", "labels": { "en": "Value", "ptBR": "Valor" } } ] }
Can someone please guide me on how to achieve this? Thx!