简体   繁体   中英

Jolt convert JSON file with data enrichment and deletion

I need help, I'm new to Jolt. There is a json file:

{
  "Date": "2021-01-01",
  "Status": "New",
  "Agreements": [
    {
      "ID_agreement": "12345",
      "ID": "fffffff",
      "balance": {
        "rub": 5,
        "usd": 6,
        "eur": 7
      },
      "withdrawal": {
        "rub": 8,
        "usd": 45,
        "eur": 6
      }
    },
    {
      "ID_agreement": "6789",
      "ID": "dddddd",
      "balance": {
        "rub": 10,
        "usd": 20,
        "eur": 30
      }
    }
  ]
}

At the output, you really need to get something like this:

{
  "type": "DATA",
  "date": "2021-01-01",
  "id_agreement": "12345",
  "id": "fffffff",
  "source": "SITE",
  "unloadDateTime": "current date if possible",
  "balance": {
    "rub": 5,
    "usd": 6,
    "eur": 7
  },
  "withdrawal": {
    "rub": 8,
    "usd": 45,
    "eur": 6
  }
},
{
  "type": "DATA",
  "date": "2021-01-01",
  "id_agreement": "6789",
  "id": "dddddd",
  "source": "SITE",
  "unloadDateTime": "current date if possible",
  "balance": {
    "rub": 10,
    "usd": 20,
    "eur": 30
}

must be added to each block:

  "type": "DATA",
  "date": "2021-01-01",
  "source": "SITE",
  "unloadDateTime": "current date if possible"

and delete

"Status": "New"

The original file is large, and the fields withdrawal/balance are somewhere there, somewhere not

my initial Jolt spec:

[
  {
    "operation": "remove",
    "spec": {
      "Status": ""
    }
  },
  {
    "operation": "shift",
    "spec": {
      "Agreements": {
        "*": "&"
      },
      "balance": {
        "*": "&"
      }
    }
  }
 ]

Hours of disassembly with the formatter did not lead to anything, the task is one-time, please help, dear colleagues!

First of all, the object balance located at the bottom part is redundant. After removing that, apply # notation to provide those fixed values you want updating the current shift transformation such as

[
  {
    "operation": "remove",
    "spec": {
      "Status": ""
    }
  },
  {
    "operation": "shift",
    "spec": {
      "Agreements": {
        "*": {
          "#DATA": "[&1].type",
          "#2021-01-01": "[&1].date",
          "#SITE": "[&1].source",
          "#current date if possible": "[&1].unloadDateTime",
          "*": {
            "@": "[&2].&1"
          }
        }
      }
    }
  }
 ]

在此处输入图像描述

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