简体   繁体   中英

JOLT JSON Transformation - Filter Array by Dynamic Attribute Value Not Working

The filtering of JSON array works fine if we specify a static search value. This does not work if we apply a dynamic value.

Tried by dynamically passing the value.

Added below the input json along with the jolt spec for your reference.

Input:

{
  "selected_key": "key2",
  "keys": [
    {
      "name": "value1",
      "key": "key1"
    },
    {
      "name": "value21",
      "key": "key2"
    },
    {
      "name": "value22",
      "key": "key2"
    },
    {
      "name": "value3",
      "key": "key3"
    },
    {
      "name": "value4",
      "key": "key4"
    }
  ]
}

JOLT Spec:


[
  {
    "operation": "shift",
    "spec": {
      "keys": {
        "*": {
          "key": {
            "@(4,selected_key)": {
              "@2": "test"
            }
          }
        }
      }
    }
  }
]

Expected Output:


{
  "test": [
    {
      "name": "value21",
      "key": "key2"
    },
    {
      "name": "value22",
      "key": "key2"
    }
  ]
}

Output: The spec is not filtering any value and all the array items are added to the output json

{
  "test": [
    {
      "name": "value1",
      "key": "key1"
    },
    {
      "name": "value21",
      "key": "key2"
    },
    {
      "name": "value22",
      "key": "key2"
    },
    {
      "name": "value3",
      "key": "key3"
    },
    {
      "name": "value4",
      "key": "key4"
    }
  ]
}

You can use two consecutive shift transformation spec such as

[
  {// tag the objects or array of objects by common "key" values
    "operation": "shift",
    "spec": {
      "*": "&",
      "keys": {
        "*": {
          "@": "@(1,key)"
        }
      }
    }
  },
  {// rename "key2" with "test"
    "operation": "shift",
    "spec": {
      "selected_key": {
        "*": {
          "@(2,&)": "test"
        }
      }
    }
  }
]

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM