用于三元操作的JOLT规范

时间:2019-08-29 20:49:00

标签: jolt

我正在尝试编写颠簸规范,以将以下输入转换为下面提到的预期输出

输入:

{ "city": "Seattle", "state": "WA", "country": "US", "date": "10/20/2018", "userList": [ { "name": "David", "age": "22", "sex": "M", "company": "good" }, { "name": "Tom", "age": "30", "sex": "M", "company": "good" }, { "name": "Annie", "age": "25", "sex": "F", "company": "bad" }, { "name": "Aaron", "age": "27", "sex": "M", "company": "bad" } ] }

预期输出:

{ "users" : [ { "date" : "10/20/2018", "username" : "David", "age" : "22", "sex" : "M", "organization" : "good" }, { "date" : "10/20/2018", "username" : "Tom", "age" : "30", "sex" : "M", "organization" : "good" } ], "Date" : "10/20/2018", "State" : "WA", "Country" : "US" }

我想过滤掉列表中company = bad或sex = F的所有元素,或者只保留company = good和sex = M的元素。 在基于特定条件从列表中删除元素时,我需要帮助。 对于这种以数据为依据的转换,建议使用震动吗?

到目前为止我写的规范是 [{ "operation": "shift", "spec": { "userList": { "*": { "name": "users.[&1].username", "age": "users.[&1].age", "sex": "users.[&1].sex", "company": "users.[&1].organization", "@(2,date)": "users.[&1].date" } }, "date": "Date", "state": "State", "country": "Country" } } ]

1 个答案:

答案 0 :(得分:0)

好的,这是我遵循的过程,以提出“把所有M +好”作为解决方案

  1. 按原样复制顶级元素,不影响范围
  2. 要获取用户内容,请转到用于过滤“用户-> *->性别-> M”的第一个字段,然后复制这种情况下的所有字段
  3. 对用于过滤的其他字段执行相同的操作
  4. 在步骤2和3中,我使用了一个映射来避免数组中的“ null”。现在切换回数组
[{
    "operation": "shift",
    "spec": {
      "userList": {
        "*": {
          "name": "users.[&1].username",
          "age": "users.[&1].age",
          "sex": "users.[&1].sex",
          "company": "users.[&1].organization",
          "@(2,date)": "users.[&1].date"
        }
      },
      "date": "Date",
      "state": "State",
      "country": "Country"
    }
    }
  ,
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "users": {
        "*": {
          "sex": {
            "M": {
              "@(2,username)": "users.&3.username",
              "@(2,age)": "users.&3.age",
              "@(2,sex)": "users.&3.sex",
              "@(2,organization)": "users.&3.organization",
              "@(2,date)": "users.&3.date"
            }
          }
        }
      }
    }
   }
  ,
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "users": {
        "*": {
          "organization": {
            "good": {
              "@(2,username)": "users.&3.username",
              "@(2,age)": "users.&3.age",
              "@(2,sex)": "users.&3.sex",
              "@(2,organization)": "users.&3.organization",
              "@(2,date)": "users.&3.date"
            }
          }
        }
      }
    }
     },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "users": {
        "*": "users[]"
      }
    }
 }
]

对于其他情况,您只需要更改过滤器规则即可在规则匹配时不执行任何操作,而在其他情况下(*)复制该字段。给出以下说明(仅在第一个字段上进行,对第二个字段进行更新应该不是问题)

[{
    "operation": "shift",
    "spec": {
      "userList": {
        "*": {
          "name": "users.[&1].username",
          "age": "users.[&1].age",
          "sex": "users.[&1].sex",
          "company": "users.[&1].organization",
          "@(2,date)": "users.[&1].date"
        }
      },
      "date": "Date",
      "state": "State",
      "country": "Country"
    }
    }
  ,
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "users": {
        "*": {
          "sex": {
            "F": null,
            "*": {
              "@(2,username)": "users.&3.username",
              "@(2,age)": "users.&3.age",
              "@(2,sex)": "users.&3.sex",
              "@(2,organization)": "users.&3.organization",
              "@(2,date)": "users.&3.date"
            }
          }
        }
      }
    }
   },

  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "users": {
        "*": "users[]"
      }
    }
 }
]