使用 Jolt 规范根据子属性的值重命名属性



我需要更改 JSON 属性的名称,但前提是子属性的值是特定值。在下面的示例中,我想将data.relationships.policyHolder重命名为data.relationships.companies但前提是data.relationships.policyHolder.data.type等于"companies"。颠簸能做这样的事情吗?

输入

{
"data": {
"type": "clients",
"id": "U5QYNSHY27CPD6DBQL2PRV6EU2BIJZ6G6STH47Q-",
"relationships": {
"policyHolder": {
"data": {
"type": "companies",
"id": "U5QYNSHY27CPD6DBQL2PRV6EU34NPSGEQKCOPRXUUZ7H4---"
}
},
"persons": {
"data": {
"type": "persons",
"id": "6H4IN45HMHCKN6MG27ZE5V6EU34NPRVD6GB6PEI-"
}
}
}
}

}

输出

{
"data": {
"type": "clients",
"id": "U5QYNSHY27CPD6DBQL2PRV6EU2BIJZ6G6STH47Q-",
"relationships": {
"companies": {
"data": {
"type": "companies",
"id": "U5QYNSHY27CPD6DBQL2PRV6EU34NPSGEQKCOPRXUUZ7H4---"
}
},
"persons": {
"data": {
"type": "persons",
"id": "6H4IN45HMHCKN6MG27ZE5V6EU34NPRVD6GB6PEI-"
}
}
}
}

是的,这有点丑陋,但它有效。

规范

[
{
"operation": "shift",
"spec": {
"data": {
// pass type and id thru
"type": "data.type",
"id": "data.id",
"relationships": {
"*": { // policyHolder, persons, etc
"data": {
"type": {
// if the type is companies
"companies": {
// then grab the whole blob of json at the "data" level
//  and copy it to the outut at "data.relationships.companies"
"@3": "data.relationships.companies"
},
// all other values of "type", just pass the "data" blob thru
//  at the same path in the output.
"*": {
"@3": "data.relationships.&4"
}
}
}
}
}
}
}
}
]

最新更新