0
votes

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?

1
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
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

1 Answers

1
votes

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.