在 jolt 中复制同一数组中的对象,并更改键名称

问题描述 投票:0回答:1

我想复制数组中的一些对象并将其放入同一个数组中。我还想更改新对象的密钥。我只想在它与数组本身中的某个值匹配时才进行复制。

输入规格

{
  "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"
          }
        ]
      }
    ]
  }
}

我想要的输出

{
  "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"
          }
        ]
      }
    ]
  }
}

我用过的规格。任何人都可以进一步帮助它..

[
  {
    // 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]"
                  
              }
            }
          }
        }
      }
    }
  }]

我想复制键名为“user-prefs.conditions.rlThreshold”的对象。我只想在键为 stoplevel.requested 或其他值时复制。如果值是 stoplevel.appointment,我不想复制。 我已经尝试了几次震动查询,但无法做到这一点..任何人都可以帮助查询

transform jolt
1个回答
0
投票

你可以这样做:

[
  { //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,&))"
            }
          }
        }
      }
    }
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.