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

时间:2017-07-12 08:33:29

标签: json jolt

我需要更改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-"
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

是的,这有点难看,但它确实有效。

规格

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