Search code examples
jsontransformjolt

Copy object in same array in jolt with key name change


I want to copy some piece of object in array and put it in same array. Also i want to change key of new object. I want to duplicate only if it matches with some value which is there in array itself.

Input spec

{
  "rule": {
    "criteria": [
      {
        "condition": {
          "id": 3375438,
          "key": "user-prefs.conditions.rlThreshold",
          "value": "RL Threshold"
        },
        "function": {
          "id": 3375443,
          "key": "filterfunction-gte",
          "value": "is greater than or equal to"
        },
        "values": [
          {
            "value": 22
          }
        ]
      },
      {
        "condition": {
          "id": 3374933,
          "key": "user-prefs.conditions.default-stop",
          "value": "Stop type"
        },
        "function": {
          "id": 2902347,
          "key": "filterfunction-eq",
          "value": "is"
        },
        "values": [
          {
            "id": 100056,
            "key": "stoplevel.requested",
            "value": "Must arrive by date"
          }
        ]
      }
    ]
  }
}

Output i want

{
  "rule": {
    "criteria": [
      {
        "condition": {
          "id": 3375438,
          "key": "user-prefs.conditions.rlThreshold",
          "value": "RL Threshold"
        },
        "function": {
          "id": 3375443,
          "key": "filterfunction-gte",
          "value": "is greater than or equal to"
        },
        "values": [
          {
            "value": 22
          }
        ]
      },

      {
        "condition": {
          "id": 3375438,
          "key": "user-prefs.conditions.rlThreshold-new",
          "value": "RL Threshold"
        },
        "function": {
          "id": 3375443,
          "key": "filterfunction-gte",
          "value": "is greater than or equal to"
        },
        "values": [
          {
            "value": 22
          }
        ]
      },
      {
        "condition": {
          "id": 3374933,
          "key": "user-prefs.conditions.default-stop",
          "value": "Stop type"
        },
        "function": {
          "id": 2902347,
          "key": "filterfunction-eq",
          "value": "is"
        },
        "values": [
          {
            "id": 100056,
            "key": "stoplevel.requested",
            "value": "Must arrive by date"
          }
        ]
      }
    ]
  }
}

spec i have used. can anyone help further on it..

[
  {
    // default in the new "thing first"
    "operation": "default",
    "spec": {
      "temp": {
        "condition": {
          "id": 3375438,
          "key": "user-prefs.conditions.rlThreshold-new",
          "value": "RL Threshold"
        },
        "function": {
          "id": 3375443,
          "key": "filterfunction-gte",
          "value": "is greater than or equal to"
        },
        "values": [
          {
            "value": 22
        }
      ]
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "rule": {
        "criteria": {
          "*": {
            "*": "&3.&2[&1].&",
            "values": {
              "*": {
                "*": "&5.[&4].&3[&2]"
                  
              }
            }
          }
        }
      }
    }
  }]

I wan to copy this object with key name "user-prefs.conditions.rlThreshold". And i only want to copy only if key is stoplevel.requested or other value. I don't want to copy if values is stoplevel.appointment. I Have tried several jolt queries but couldn't do that.. can anyone help with query


Solution

  • You can do it like this :

    [
      { //generate object keys by condition.key values 
        "operation": "shift",
        "spec": {
          "rule": {
            "criteria": {
              "*": {
                "*": "&3.&2.@(1,condition.key)_&1.&"
              }
            }
          }
        }
      },
      { //generate new objects by predefined conditions along with the existing ones
        "operation": "shift",
        "spec": {
          "rule": {
            "criteria": {
              "@": "&2.&1", //replicate the existing subobjects/subarrays
              "user-prefs.conditions.rlThreshold|stoplevel.requested": { //filtering options
                "*": {
                  "*": "&4.&3.&2.new&1.&",
                  "condition": {
                    "*": "&5.&4.&3.new&2.&1.&",
                    "key": {
                      "*": {
                        "@1|#-new": "&7.&6.&5.new&4.&3.&2"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "rule": {
            "criteria": {
              "*": {
                "*": "&3.&2[]"
              }
            }
          }
        }
      },
      {//convert array type keys to attribute type
        "operation": "modify-overwrite-beta",
        "spec": {
          "rule": {
            "criteria": {
              "*": {
                "condition": {
                  "key": "=join('',@(1,&))"
                }
              }
            }
          }
        }
      }
    ]
    

    the demo on the site https://jolt-demo.appspot.com/ is :

    enter image description here