0

I have a need to change the name of JSON property but only if the value of a sub-property is a specific value. In the example below I want to rename data.relationships.policyHolder to data.relationships.companies but only if data.relationships.policyHolder.data.type equals "companies". Can jolt do something like this?

Input

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

}

Output

{
"data": {
    "type": "clients",
    "id": "U5QYNSHY27CPD6DBQL2PRV6EU2BIJZ6G6STH47Q-",
    "relationships": {
        "companies": {
            "data": {
                "type": "companies",
                "id": "U5QYNSHY27CPD6DBQL2PRV6EU34NPSGEQKCOPRXUUZ7H4---"
            }
        },
        "persons": {
            "data": {
                "type": "persons",
                "id": "6H4IN45HMHCKN6MG27ZE5V6EU34NPRVD6GB6PEI-"
            }
        }
    }
}
Shane Rowatt
  • 1,951
  • 3
  • 27
  • 44

1 Answers1

0

Yes, it is a tad ugly, but it works.

Spec

[
  {
    "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"
                }
              }
            }
          }
        }
      }
    }
  }
]
Milo S
  • 4,466
  • 1
  • 19
  • 22