简体   繁体   中英

Transform JSON-JSON JOLT

I am quite new to JOLT and I need to transform my JSON files to the desired schema. This is my input

[
    {
        "PK": 12345,
        "FULL_NAME":"Amit Prakash",
        "BIRTHDATE":"1987-05-25",
        "SEX":"M",
        "EMAIL": "amprak@mail.com",
        "PHONE": "809386731",
        "TS":"2015-11-19 14:36:34.0"
    },
    {
        "PK": 12654,
        "FULL_NAME": "Rohit Dhand",
        "BIRTHDATE":"1979-02-01",
        "SEX":"M",
        "EMAIL": "rodha@mail.com",
        "PHONE": "937013861",
        "TS":"2015-11-20 11:03:02.6"
    },
    ...
]

and this is my desired output:

{
    "records": [
        {
            "attribs": [{
                "type": "customer",
                "reference": "CUST"
            }],
            "name": "Amit Prakash",
            "personal_email": "amprak@mail.com",
            "mobile": "809386731",
            "id": 12345 
        },
        {
            "attribs": [{
                "type": "customer",
                "reference": "CUST"
            }],
            "name": "Rohit Dhand",
            "personal_email": "rodha@mail.com",
            "mobile": "937013861",
            "id": 12654 
        },
        ...
    ]
}

So far, I have only managed up to this point:

[
  {
    "operation": "remove",
    "spec": {
      "*": {
        "BIRTHDATE": "",
        "SEX": "",
        "TS": ""
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "records"
    }
  }
]

But I can't go on from here. I don't know how to rename keys in the output.

Also, what's the alternative to remove operation? remove operation is good if you have fewer keys to exclude than to include, but how about the reverse (few keys to include, more than to exclude within a JSON object)?

Spec

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "PK": "records[&1].id",
        "PHONE": "records[&1].mobile",
        "EMAIL": "records[&1].personal_email",
        "FULL_NAME": "records[&1].name"
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "records[]": {
        "*": {
          "attribs[]": {
            "0": {
              "type": "customer",
              "reference": "CUST"
            }
          }
        }
      }
    }
  }
]

Shift makes copy of your data, whereas the other operations do not. Thus, one way to remove stuff is to just not have it copied across in your shift spec.

Generally the remove is used to get rid of things that would "mess up" the shift.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM