繁体   English   中英

基于 json 中的另一个条件值的颠簸变换

[英]Jolt transformation on basis of another conditional value in the json

我正在尝试使用 jolt 来转换 json 有效负载,我的输入 json 看起来像这样

{
  "root": {
    "entities": [
      {
        "fields": [
          {
            "name": "ContactType",
            "value": "Company"
          },
          {
            "name": "HandlerName",
            "value": "XYZ"
          },
          {
            "name": "Reference",
            "value": "123-4443"
          },
          {
            "entities": [
              {
                "parent": {
                  "fieldName": "DefendantAddress"
                },
                "fields": [
                  {
                    "name": "Address",
                    "value": "1662369113138 Somewhere Street"
                  },
                  {
                    "name": "PostalCode",
                    "value": "XXXXX"
                  }
                ]
              }
            ],
            "name": "DefendantAddress"
          }
        ]
      }
    ]
  }
}

我希望根据 ContactType 字段中的值转换地址数据。

如果contact typeCompany ,则 output 应该是

{
  "HandlerName": "XYZ",
  "reference": "123-4443",
  "companyDetails": {
    "address": "1662369113138 Somewhere Street",
    "postalCode": "XXXXX"
  }
}

如果contact typePerson ,那么 output 应该是

{
  "HandlerName": "XYZ",
  "reference": "123-4443",
  "personDetails": {
    "address": "1662369113138 Somewhere Street",
    "postalCode": "XXXXX"
  }
}

我可以使用我的 jolt 规范检查联系人类型是否为公司或个人,但难以在层次结构中导航以到达子实体。 这是我的颠簸规格。 我要执行的是??? 区域或是否可以针对该问题提供其他解决方案。

[
  {
    "operation": "shift",
    "spec": {
      "root": {
        "entities": {
          "*": {
            "fields": {
              "*": {
                "name": {
                  "HandlerName": {
                    "@(2,value)": "handlerName"
                  },
                  "Reference": {
                    "@(2,value)": "reference"
                  },
                  "ContactType": {
                    "@(2,value)": {
                      "Company": {
                        ?????
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

您可以从深入研究最里面的对象开始,以便在此级别匹配valuename属性( "@(0,value)": "personDetails.@(0,name)" ),然后从外部的 object 在经过一些( 6 )级之后到达树的水平。 在即将到来的班次转换规范中使用条件,例如

[
  {
    "operation": "shift",
    "spec": {
      "root": {
        "entities": {
          "*": {
            "fields": {
              "*": {
                "entities": {
                  "*": {
                    "fields": {
                      "*": {
                        "@(6,fields)": { //traverse "{" characters 6 times to reach the level of outer "fields" tag
                          "*": {
                            "@(0,value)": "common.@(0,name)"
                          }
                        },
                        "@(0,value)": "p.@(0,name)"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "common": {
        "*": "ONE"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "common": {
        "ContactType": {
          "Company|Person": { // "|" is "OR" operator
            "@(2,HandlerName)": "HandlerName",
            "@(2,Reference)": "reference",
            "@(3,p)": "personDetails"
          }
        }
      }
    }
  }
]

https://jolt-demo.appspot.com/网站上的演示是:

在此处输入图像描述

暂无
暂无

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

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