0

I have json input with array:

{
    "id": 3,
    "name": "ROOT",
    "mylist": [{
        "id": 10,
        "info": "hello"
        },
        {
        "id": 11,
        "info": "world"
        }
    ]
}

That i would like to transform into "flat" json like the following:

{
    "id": 3,
    "name": "Root",
    "mylist[0].id": 10,
    "mylist[0].info": "hello",
    "mylist[1].id": 11,
    "mylist[1].info": "world"    
}

How can i achieve that with JoltTransformJSON spec?

vic
  • 45
  • 2
  • 9
  • What do you mean flat JSON? you have just renamed `mylist` to `record` so it now looks like : `{ "id" : 3, "name" : "Root", "record" : [{ "id" : 10, "info" : "hello"}, {"id" : 11, "info" : "world"}]}` – Sivaprasanna Sethuraman Sep 04 '18 at 03:39
  • name of array doesn't matter really, so here it is:{ "id": 3, "name": "Root", "mylist[0].id": 10, "mylist[0].info": "hello", "mylist[1].id": 11, "mylist[1].info": "world" } – vic Sep 04 '18 at 13:15

1 Answers1

1

Have to escape all the things that would normally trigger shift to add sub-object.

[
  {
    "operation": "shift",
    "spec": {
      "id": "id",
      "name": "name",
      "mylist": {
        "*": {
          "id": "myList\\[&1\\]\\.id",
          "info": "myList\\[&1\\]\\.info"
        }
      }
    }
  }
]

This is the same thing but "more generic".

[
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "mylist": {
        "*": {
          "*": "&2\\[&1\\]\\.&"
        }
      }
    }
  }
]

Note, you still have to tell it that "mylist" is a thing that needs to be stepped into, i.e. there is no Shift spec that will flatten any and all Json.

Milo S
  • 4,466
  • 1
  • 19
  • 22