最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

json - Is it possible to transform records by filtering and removing certain fields? - Stack Overflow

programmeradmin1浏览0评论

I need to have only this record with account, data(email,firstName,lastName) or city, i don't need records with idNumber and accountNumber.

Additionally, I need each record to have a common part: messageId, markerId, dateFrom and dateTo.

Is it possible to do something like that with jolt transformation?

My JSON input:

{
  "messageId": 1234,
  "markerId": "T",
  "dateFrom": 6436058131202690000,
  "dateTo": -3840351829778683400,
  "records": [
    {
      "recordId": 1,
      "account": "152739203233"
    },
    {
      "recordId": 2,
      "data": {
        "email": "[email protected]",
        "firstName": "John",
        "lastName": "Smith"
      }
    },
    {
      "recordId": 3,
      "city": "Los Angeles"
    },
    {
      "recordId": 4,
      "idNumber": "12345"
    },
    {
      "recordId": 5,
      "accountNumber": "55671"
    },
    {
      "recordId": 6,
      "account": "6789189790191"
    },
    {
      "recordId": 7,
      "city": "San Fransisco"
    }
  ]
}

And I would like to have output like that:

[
  {
    "messageId": 1234,
    "markerId": "T",
    "dateFrom": 6436058131202690000,
    "dateTo": -3840351829778683400,
    "recordId": 1,
    "account": "152739203233"
  },
  {
    "messageId": 1234,
    "markerId": "T",
    "dateFrom": 6436058131202690000,
    "dateTo": -3840351829778683400,
    "recordId": 2,
    "email": "[email protected]",
    "firstName": "John",
    "lastName": "Smith"
  },
  {
    "messageId": 1234,
    "markerId": "T",
    "dateFrom": 6436058131202690000,
    "dateTo": -3840351829778683400,
    "recordId": 3,
    "city": "Los Angeles"
  },
  {
    "messageId": 1234,
    "markerId": "T",
    "dateFrom": 6436058131202690000,
    "dateTo": -3840351829778683400,
    "recordId": 6,
    "account": "6789189790191"
  },
  {
    "messageId": 1234,
    "markerId": "T",
    "dateFrom": 6436058131202690000,
    "dateTo": -3840351829778683400,
    "recordId": 7,
    "city": "San Fransisco"
  }
]

I need to have only this record with account, data(email,firstName,lastName) or city, i don't need records with idNumber and accountNumber.

Additionally, I need each record to have a common part: messageId, markerId, dateFrom and dateTo.

Is it possible to do something like that with jolt transformation?

My JSON input:

{
  "messageId": 1234,
  "markerId": "T",
  "dateFrom": 6436058131202690000,
  "dateTo": -3840351829778683400,
  "records": [
    {
      "recordId": 1,
      "account": "152739203233"
    },
    {
      "recordId": 2,
      "data": {
        "email": "[email protected]",
        "firstName": "John",
        "lastName": "Smith"
      }
    },
    {
      "recordId": 3,
      "city": "Los Angeles"
    },
    {
      "recordId": 4,
      "idNumber": "12345"
    },
    {
      "recordId": 5,
      "accountNumber": "55671"
    },
    {
      "recordId": 6,
      "account": "6789189790191"
    },
    {
      "recordId": 7,
      "city": "San Fransisco"
    }
  ]
}

And I would like to have output like that:

[
  {
    "messageId": 1234,
    "markerId": "T",
    "dateFrom": 6436058131202690000,
    "dateTo": -3840351829778683400,
    "recordId": 1,
    "account": "152739203233"
  },
  {
    "messageId": 1234,
    "markerId": "T",
    "dateFrom": 6436058131202690000,
    "dateTo": -3840351829778683400,
    "recordId": 2,
    "email": "[email protected]",
    "firstName": "John",
    "lastName": "Smith"
  },
  {
    "messageId": 1234,
    "markerId": "T",
    "dateFrom": 6436058131202690000,
    "dateTo": -3840351829778683400,
    "recordId": 3,
    "city": "Los Angeles"
  },
  {
    "messageId": 1234,
    "markerId": "T",
    "dateFrom": 6436058131202690000,
    "dateTo": -3840351829778683400,
    "recordId": 6,
    "account": "6789189790191"
  },
  {
    "messageId": 1234,
    "markerId": "T",
    "dateFrom": 6436058131202690000,
    "dateTo": -3840351829778683400,
    "recordId": 7,
    "city": "San Fransisco"
  }
]
Share edited Feb 11 at 10:07 DarkBee 15.6k8 gold badges72 silver badges116 bronze badges asked Feb 10 at 11:15 Sophie78Sophie78 212 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Please try the below jolt spec

{
    "operation": "modify-overwrite-beta",
    "spec": {
      "records": {
        "*": {
          "isIdNumber": ["=isPresent(@(1,idNumber))", false],
          "isAccountNumber": ["=isPresent(@(1,accountNumber))", false]
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "records": {
        "*": {
          "isIdNumber": {
            "false": {
              "@(2,isAccountNumber)": {
                "false": {
                  "@(4,recordId)": "&6.&5.recordId",
                  "@(4,account)": "&6.&5.account",
                  "@(6,messageId)": "&6.&5.messageId",
                  "@(6,markerId)": "&6.&5.markerId",
                  "@(6,dateFrom)": "&6.&5.dateFrom",
                  "@(6,dateTo)": "&6.&5.dateTo",
                  "@(4,data)": {
                    "*": "&7.&6.&"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "records": {
        "*": {
          "*": "[#2].&"
        }
      }
    }
  }

You can use the following transformation :

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "records": {
        "*": {
          "~idNumber|~accountNumber": "-1"//create the temporary attributes for the objects which currently don't have them
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "others.&",
      "records": { //loop through the "records" array
        "*": { //indexes of the "records" array
          "*": "&2.&1.@1,idNumber.@1,accountNumber.&"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "records": {
        "*": {
          "-1": {//only pick the attributes without
            "-1": {//idNumber or accountNumber
              "@4,others": { "*": "&4.&" },
              "*": "&3.&",
              "idNumber|accountNumber": { "": "" }//get rid of the temporary attributes
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "[]"
    }
  }
]
发布评论

评论列表(0)

  1. 暂无评论