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

Rename property depending on value of sub-property using a Jolt spec

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

输入

{
"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"
                }
              }
            }
          }
        }
      }
    }
  }
]