[英]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 type
是Company
,則 output 應該是
{
"HandlerName": "XYZ",
"reference": "123-4443",
"companyDetails": {
"address": "1662369113138 Somewhere Street",
"postalCode": "XXXXX"
}
}
如果contact type
是Person
,那么 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": {
?????
}
}
}
}
}
}
}
}
}
}
}
]
您可以從深入研究最里面的對象開始,以便在此級別匹配value
和name
屬性( "@(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.