bazaarvoice/jolt

Transforming a Array into Array with pairs

leafpeak opened this issue · 5 comments

I just started checking out jolt but I am having a bit of trouble on transforming an array into an array with objects.

Input:

{
    "ids": [1, 2],
    "message": "Hello"
}

Expected:

[
     {
         "user": {
             "id": 1
         },
         "message": "Hello"
     },
     {
         "user": {
             "id": 2
         },
        "message": "Hello"
     }
]

Working on my spec so far:

[
  {
    "operation": "shift",
    "spec": {
      "ids": {
        "*": {
          "@": "[#2].user.id"
        }
      }
    }
  }
]

Having a tough time on trying get a message injected into each object

It doesn't seem that key look up works? Or am I using this incorrectly?

  [{
    "operation": "shift",
    "spec": {
      "ids": {
        "*": {
          "@": "[#2].user.id",
          "@(3, message)": "[#2].message"
        }
      }
    }
  }
]

I was able to create many permutations depending on the ids however I need to now ids unique and flatten the array with the following spec

[
  {
    "operation": "shift",
    "spec": {
      "ids": {
        "*": {
          "*": {
            "@(3)": "[]"
          }
        }
      }
    }
  }
]

Spec

[
  {
    "operation": "shift",
    "spec": {
      "ids": {
        "*": { // match all array indicies
          // write the value at this array index to
          //  "[&1].user.id", where &1 is the "current"
          //  value of the "ids" arrat
          "@": "[&1].user.id",

          // while we've matched each array index of 
          //  the ids array :
          // Look back up the tree 3 levels (0,1,2), 
          //  and the come back down and grab the value 
          //  at "message" and write it to 
          //  "[&1].message" in the output.
          "@(2,message)": "[&1].message"
        }
      }
    }
  }
]

@milosimpson thanks! I was really close to getting this to work! however for some reason I am so used to adding a space after a comma and it seems to break when trying to do a lookup.

"@(2, message)": "..."