qntfy / kazaam

Arbitrary transformations of JSON in Golang
MIT License
279 stars 55 forks source link

Help with transform spec #60

Closed rodolfoag closed 7 years ago

rodolfoag commented 7 years ago

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!

ryanleary commented 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.

ryanleary commented 7 years ago

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
rodolfoag commented 7 years ago

Works like a charm @ryanleary! Thx mate.

ryanleary commented 7 years ago

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.

rodolfoag commented 7 years ago

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!

rodolfoag commented 7 years ago

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!

rodolfoag commented 7 years ago

Forget my last comment! For every element in items.metadata, kazaam replaced labels with its labels[0]. As it should be.