首页 > 解决方案 > 如何通过几个键使用 JOLT Transform 过滤数据

问题描述

我正在尝试使用 Jolt 转换,但努力让它发挥作用。

如果我有这样的输入:

 {"options": [
{
  "id": "18031",
  "name": "sample",
  "archived": true,
  "released": true,
  "releaseDate": "2014-11-04",
  "userReleaseDate": "04 Nov 2014",
  "projectId": 13001
},
{
  "id": "231418",
  "description": "service_release",
  "name": "3.07.17",
  "archived": false,
  "flag_m": true,
  "releaseDate": "2017-07-03",
  "userReleaseDate": "03 Jul 2017",
  "projectId": 13001
},
{
  "id": "249700",
  "description": "service_release",
  "name": "service-09.02.18",
  "archived": false,
  "flag_m": false,
  "startDate": "2018-02-09",
  "userStartDate": "09 Feb 2018",
  "projectId": 13001
}]}

我希望输出为:

 {
  "options" : [
   {
    "value" : "service-09.02.18",
    "key" : "service-09.02.18"
    }, 
    {
    "value" : "3.07.17",
    "key" : "3.07.17"  
    } 
   ]
  }

仅适用于 flag_m=false 和 description=service_release 的对象

Jolf 变换有可能吗?

标签: jsonjolt

解决方案


规格和评论

[
  {
    "operation": "shift",
    "spec": {
      "options": {
        "*": {
          "flag_m": {
            "false": {
              // match 'flag_m = false', and if we get 
              //  here, reset up the tree so we can 
              //  check 'description = service_release'
              "@2": {
                "description": {
                  "service_release": {
                    // If we matched all the way down here,
                    // then reset up again, and grab the data.
                    "@5": {
                      // Your example output did not make sense to me.
                      // Assuming you want the id as a "key",
                      //  and the name as "value"
                      //
                      // This builds two parallel arrays in the
                      //  output, that we can pivot in the next
                      //  step.
                      //
                      "name": "values[]",
                      "id": "keys[]"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "keys": {
        "*": "options[&].key"
      },
      "values": {
        "*": "options[&].value"
      }
    }
  }
]

推荐阅读