Search code examples
jsonjolt

Need Jolt spec for complex json to flatten and convert one field name to specific name


I have a complex input JSON which has few dynamic fields. I need jolt spec file which will allow only the specific fields in the output json and rest will be ignored. So I need to specify each field so that only those fields will be displayed in output and any extra unwanted field will be ignored. The jolt spec I write is giving me the exact date(EX: "2024-04-14")under "future" tag. But I need to convert it to a single array called "Group_Dates" and under which all the items are coming(similar like output json).

Input JSON:

{
  "metadata": {
    "version": "1.0"
  },
  "message": {
    "id": "2067e529d028-098",
    "code": "PROP",
    "type": "notify",
    "POST": {
      "id": "2067e529d028-cf23",
      "code": "PROP",
      "date": "2024-04-12T00:00:00.000Z",
      "summary": [
        {
          "actual": 3,
          "type": "adults"
        },
        {
          "actual": 5,
          "type": "cancelled"
        }
      ],
      "payments": [
        {
          "actual": 2068.8,
          "type": "CASH",
          "name": "CASH"
        },
        {
          "actual": 0,
          "type": "CP",
          "name": "PAYMENT"
        }
      ],
      "market": [
        {
          "code": "D",
          "name": "DISCOUNT",
          "date": "2024-04-12T00:00:00.000Z"
        },
        {
          "code": "G",
          "name": "REGULAR",
          "date": "2024-04-12T00:00:00.000Z"
        }
      ],
      "future": {
        "2024-04-13": [
          {
            "code": "G",
            "name": "CORPORATE",
            "date": "2024-04-13T00:00:00.000Z",
            "rev": 598,
            "sold": 2
          }
        ],
        "2024-04-14": [
          {
            "code": "G",
            "name": "REGULAR",
            "date": "2024-04-14T00:00:00.000Z",
            "rev": 1388.32,
            "sold": 5
          },
          {
            "code": "D",
            "name": "DISCOUNT",
            "date": "2024-04-14T00:00:00.000Z",
            "rev": 284.05,
            "sold": 1
          },
          {
            "code": "Q",
            "name": "GOVERNMENT",
            "date": "2024-04-14T00:00:00.000Z",
            "rev": 264.33,
            "sold": 3
          }
        ]
      },
      "forecast": [
        {
          "total": 97,
          "dooms": 0
        },
        {
          "total": 97,
          "dooms": 0
        }
      ],
    "timezone": "CST6CDT",
    "change": [
      ""
    ],
    "time": "2024-04-14T11:39:22.522Z",
    "time_zone": "UTC-05:00",
    "date": "2024-04-12T00:00:00.000Z"
    }
  }
}

Output Json:

{
  "metadata": {
    "version": "1.0"
  },
  "message": {
    "id": "2067e529d028-098",
    "code": "PROP",
    "type": "notify",
    "POST": {
      "id": "2067e529d028-cf23",
      "code": "PROP",
      "date": "2024-04-12T00:00:00.000Z",
      "summary": [
        {
          "actual": 3,
          "type": "adults"
        },
        {
          "actual": 5,
          "type": "cancelled"
        }
      ],
      "payments": [
        {
          "actual": 2068.8,
          "type": "CASH",
          "name": "CASH"
        },
        {
          "actual": 0,
          "type": "CP",
          "name": "PAYMENT"
        }
      ],
      "market": [
        {
          "code": "D",
          "name": "DISCOUNT",
          "date": "2024-04-12T00:00:00.000Z"
        },
        {
          "code": "G",
          "name": "REGULAR",
          "date": "2024-04-12T00:00:00.000Z"
        }
      ],
      "forecast": [
        {
          "total": 97,
          "dooms": 0
        },
        {
          "total": 97,
          "dooms": 0
        }
      ],
      "future": {
        "Group_Dates": [
          {
            "code": "G",
            "name": "CORPORATE",
            "date": "2024-04-13T00:00:00.000Z",
            "revenue": 598,
            "sold_count": 2
          },
          {
            "code": "G",
            "name": "REGULAR",
            "date": "2024-04-14T00:00:00.000Z",
            "revenue": 1388.32,
            "sold_count": 5
          },
          {
            "code": "D",
            "name": "DISCOUNT",
            "date": "2024-04-14T00:00:00.000Z",
            "revenue": 284.05,
            "sold_count": 1
          },
          {
            "code": "Q",
            "name": "GOVERNMENT",
            "date": "2024-04-14T00:00:00.000Z",
            "revenue": 264.33,
            "sold_count": 3
          }
        ]
      },
      "timezone": "CST6CDT",
      "change": [
        ""
      ],
      "time": "2024-04-14T11:39:22.522Z",
      "time_zone": "UTC-05:00"
    }
  }
}

Jolt Spec:

[
  {
    "operation": "shift",
    "spec": {
      "metadata": {
        "version": ["metadata.version"],
        "type": ["metadata.type"],
        "source": ["metadata.source"],
        "contentEncoding": ["metadata.contentEncoding"],
        "guid": ["metadata.guid"]
      },
      "message": {
        "id": ["message.id"],
        "code": ["message.code"],
        "type": ["message.type"],
        "POST": {
          "id": ["message.POST.id"],
          "code": ["message.POST.code"],
          "date": ["message.POST.date"],
          "summary": {
            "*": {
              "actual": "message.POST.summary[&1].actual",
              "type": "message.POST.summary[&1].type"
            }
          },
          "payments": {
            "*": {
              "actual": "message.POST.payments[&1].actual",
              "type": "message.POST.payments[&1].type",
              "name": "message.POST.payments[&1].name"
            }
          },
          "market": {
            "*": {
              "code": "message.POST.market[&1].code",
              "name": "message.POST.market[&1].name",
              "date": "message.POST.market[&1].date"
            }
          },
          "future": {
            "*": {
              "*": {
                "code": "&3.&2_&1.code",
                "name": "&3.&2_&1.name",
                "date": "&3.&2_&1.date",
                "rev": "&3.&2_&1.revenue",
                "sold": "&3.&2_&1.sold_count"
              }
            }
          },
          "forecast": {
            "*": {
              "total": "message.POST.forecast[&1].total",
              "dooms": "message.POST.forecast[&1].dooms"
            }
          }       
         },
          "timezone": ["message.timezone"],
          "change": ["message.change"],
          "time": ["message.time"],
          "time_zone": ["message.time_zone"],
          "date": ["message.date"]        
      }
    }
  }
]

Solution

  • You can use the following shift transformation spec:

    [
      {
        "operation": "shift",
        "spec": {
          "*": "&",// the objects(or arrays) other than "message"(it's only "metadata" in this case)
          "message": {
            "POST": {
              "*": "&2.&1.&", // the elements other than "future" nested within "POST" object 
              "future": {
                "*": {
                  "*": "&4.&3.&2.Group_Dates[]" //&2 stands for the literal "future"
                    //&4.&3 is for "message"."POST" 
                }
              }
            }
          }
        }
      }
    ]