简体   繁体   English

颠簸变换后更改output名称

[英]Change output name after jolt transformation

I'am new in the world of jolt and i was wondering if there is good way to modify the name of the output field after the transformation我是 jolt 世界的新手,我想知道是否有好方法可以在转换后修改 output 字段的名称

Input sample:输入样本:

 {
      "userId": "1",
      "age": "20",
      "firstName": "firstname1",
      "lastname": "lastname1",
      "zipCode": "zipcode1",
      "street": "street1",
      "city": "city1",
      "country": "country",
      "gender": "gender1",
      "grade": "grade1",
      "birthday": "birthday1"
    }

Spec sample ==> Thanks to @Barbaros for his help Spec sample ==> 感谢@Barbaros 的帮助

[
  {
    "operation": "shift",
    "spec": {
      "userId": "ID",
      "*": "&",
      "street|city|zipCode|country": {
        "$": "adr_&.code",
        "@": "adr_&.value"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "adr_*": "address[]"
    }
  }
]

Output Output

{
  "ID" : "1",
  "age" : "20",
  "firstName" : "firstname1",
  "lastname" : "lastname1",
  "address" : [ {
    "code" : "zipCode",
    "value" : "zipcode1"
  }, {
    "code" : "street",
    "value" : "street1"
  }, {
    "code" : "city",
    "value" : "city1"
  }, {
    "code" : "country",
    "value" : "country"
  } ],
  "gender" : "gender1",
  "grade" : "grade1",
  "birthday" : "birthday1"
}

And the desired output could be like below.所需的 output 可能如下所示。 I mean the value of the field's code in the address, could be customized(custom-field):我的意思是地址中字段代码的值,可以自定义(自定义字段):

{
      "ID" : "1",
      "age" : "20",
      "firstName" : "firstname1",
      "lastname" : "lastname1",
      "address" : [ {
        "code" : "**custom-field1**",
        "value" : "zipcode1"
      }, {
        "code" : "**custom-field2**",
        "value" : "street1"
      }, {
        "code" : "**custom-field3**",
        "value" : "city1"
      }, {
        "code" : "**custom-field4**",
        "value" : "country"
      } ],
      "gender" : "gender1",
      "grade" : "grade1",
      "birthday" : "birthday1"
    }

Thanks in advance.提前致谢。

In this case, rephrase each attribute belonging to the address array individually by prefixing them with # wildcard per each desired custom value as they don't have common factor such as在这种情况下,通过在每个所需的自定义值前面加上#通配符来单独改写属于address数组的每个属性,因为它们没有共同的因素,例如

[
  {
    "operation": "shift",
    "spec": {
      "userId": "ID",
      "*": "&",
      "zipCode": {
        "#custom-field1": "adr_&1.code",
        "@": "adr_&.value"
      },
      "street": {
        "#custom-field2": "adr_&1.code",
        "@": "adr_&.value"
      },
      "city": {
        "#custom-field3": "adr_&1.code",
        "@": "adr_&.value"
      },
      "country": {
        "#custom-field4": "adr_&1.code",
        "@": "adr_&.value"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "adr_*": "address[]"
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is网站http://jolt-demo.appspot.com/上的演示是

在此处输入图像描述

You may consider another library Josson .您可以考虑另一个图书馆Josson

https://github.com/octomix/josson https://github.com/octomix/josson

Deserialization反序列化

Josson josson = Josson.fromJsonString(
    "{" +
    "    \"userId\": \"1\"," +
    "    \"age\": \"20\"," +
    "    \"firstName\": \"firstname1\"," +
    "    \"lastname\": \"lastname1\"," +
    "    \"zipCode\": \"zipcode1\"," +
    "    \"street\": \"street1\"," +
    "    \"city\": \"city1\"," +
    "    \"country\": \"country\"," +
    "    \"gender\": \"gender1\"," +
    "    \"grade\": \"grade1\"," +
    "    \"birthday\": \"birthday1\"" +
    "}");
    

Transformation转型

JsonNode node = josson.getNode(
    "map(ID:userId, age, firstName, lastname," +
    "    address:collect(" +
    "              map(code:'custom-field1',value:zipCode)," +
    "              map(code:'custom-field2',value:street)," +
    "              map(code:'custom-field3',value:city)," +
    "              map(code:'custom-field4',value:country))," +
    "    gender, grade, birthday)");
System.out.println(node.toPrettyString());

Output Output

{
  "ID" : "1",
  "age" : "20",
  "firstName" : "firstname1",
  "lastname" : "lastname1",
  "address" : [ {
    "code" : "custom-field1",
    "value" : "zipcode1"
  }, {
    "code" : "custom-field2",
    "value" : "street1"
  }, {
    "code" : "custom-field3",
    "value" : "city1"
  }, {
    "code" : "custom-field4",
    "value" : "country"
  } ],
  "gender" : "gender1",
  "grade" : "grade1",
  "birthday" : "birthday1"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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