Home > Software engineering >  how to use $ and * at the same level in a spec
how to use $ and * at the same level in a spec

Time:10-23

I am new to jolt and whilst i like lots of it one thing thats really hurting me right now is how to use * and $ at the same level in a spec. I have the following desired input and output. But try as i might i cannot seem to transform both the list of action ids (there are the "1" and "2" into attribute values and move the list of action data associated with the id into a sub attribute.

Input

{
  "Attr1": "Attr1_data",
  "Actions": {
    "1": [
      "Action data 1 line 1",
      "Action data 1 line 2",
      "Action data 1 line 3"
    ],
    "2": [
      "Action data 2 line 1",
      "Action data 2 line 2",
      "Action data 2 line 3"
    ]
  },
  "Attr2": "Attr2_data"
}

Desired

{
  "Attr1": "Attr1_data",
  "Action": [
    {
      "id" : "1",
      "data" : [
        "Action data 1 line 1",
        "Action data 1 line 2",
        "Action data 1 line 3"
      ]
    },
    {
      "id" : "2",
      "data" : [
        "Action data 2 line 1",
        "Action data 2 line 2",
        "Action data 2 line 3"
      ]
    }

  ],
  "Attr2": "Attr2_data"
}

using the following spec

[
  {
    "operation" : "shift",
    "spec":  {
      "Actions": {
        "*" : {
          "$": "Action[].id"
        }
      },
      "*": "&"
    }
  }
]

I can generate

{
  "Attr1": "Attr1_data",
  "Action": [
    {
      "id": "1"
    },
    {
      "id": "2"
    }
  ],
  "Attr2": "Attr2_data"
}

But try as i might i cannot seem to copy the data lines in to a new data attribute.

Can anyone pls point me in the right direction ?

CodePudding user response:

You can convert yours to the following transformation spec

[
  {
    "operation": "shift",
    "spec": {
      "*s": { // represents a tag(of an object/array/attribute) with a trailing letter "s". The reason of this reform is to be able use "Action" as the key of the inner array without repeatedly writing it.
        "*": {
          "$": "&(2,1)[#2].id", // "$" looks one level up and copies the tag name, &(2,1) goes two levels up the tree and pick the first piece separated by asterisk, [#2] goes two level up in order to reach the level of "Actions" array to combine the subelements distributed from that level in arrayic manner with "Action"(&(2,1)) label, and the leaf node "id" stands for tag of the current attribute 
          "*": "&(2,1)[#2].data"
        }
      },
      "*": "&" // the rest of the attributes(/objects/arrays) other than "Actions"
    }
  }
]

the demo on the site enter image description here

  • Related