首页 > 解决方案 > 检查数组是否为空或不颠簸转换

问题描述

我的输入如下

{
    "reports": [
        {
            "fromDate": "2020-06-01T00:00:00",
            "toDate": "2021-45-31T00:00:00",
            "cashReceipts": {
                "accountId": "cashReceipts",
                "name": "Cash Receipts",
                "value": 5080.27,
                "items": [
                    {
                        "accountId": "income",
                        "name": "Income",
                        "value": 3113.32,
                        "items": []
                    },
                    {
                        "accountId": "accountsReceivable",
                        "name": "Accounts Receivable",
                        "value": 1966.95,
                        "items": [
                            {
                                "accountId": "cashreceivable",
                                "name": "cash",
                                "value": 500.0,
                                "items": []
                            },
                            {
                                "accountId": "chequereceivalbe",
                                "name": "Check",
                                "value": 1466.95,
                                "items": []
                            }
                        ]
                    }
                ]
            },
            "cashPayments": {
                "accountId": "cashPayments",
                "name": "Cash Payments",
                "value": 6984.39,
                "items": [
                    {
                        "accountId": "expenses",
                        "name": "Expenses",
                        "value": 6755.64,
                        "items": []
                    },
                    {
                        "accountId": "costOfSales",
                        "name": "Cost of Sales",
                        "value": 228.75,
                        "items": []
                    }
                ]
            }
        }
    ],
    "reportBasis": "Cash",
    "reportInput": "Direct",
    "currency": "USD",
    "mostRecentAvailableMonth": "2021-05-31T00:00:00",
    "earliestAvailableMonth": "2019-06-01T00:00:00"
}

我需要如下输出

[
    {
        "currency": "USD",
        "accountType" : "cashReceipts",
        "accountId": "income",
        "accountName": "Income",
        "balance": 3113.32
    },
    {
        "currency": "USD",
        "accountType" : "cashReceipts",
        "accountId": "cashreceivable",
        "accountName": "cash",
        "balance": 500.0
    },
    {
        "currency": "USD",
        "accountType" : "cashReceipts",
        "accountId": "chequereceivalbe",
        "accountName": "Check",
        "balance": 1466.95
    },
    {
        "currency": "USD",
        "accountType" : "cashPayments",
        "accountId": "expenses",
        "accountName": "Expenses",
        "balance": 6755.64
    },
    {
        "currency": "USD",
        "accountType" : "cashPayments",
        "accountId": "costOfSales",
        "accountName": "Cost of Sales",
        "balance": 228.75
    }
]

我使用了 jolt 规格作为

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "reports": {
        "*": {
          "cashReceipts": {
            "accountName": "@(1,name)",
            "fromDate": "@(2,fromDate)",
            "creationTime": "@(2,toDate)",
            "currency": "@(4,currency)",
            "reportBasis": "@(4,reportBasis)",
            "accountType": "income",
            "balance": "@(1,value)",
            "bankingDate": "@(2,date)",
            "items": {
              "*": {
                "balance": "@(1,value)",
                "bankingDate": "@(2,toDate)",
                "accountName": "@(1,name)",
                "creationTime": "@(2,date)",
                "currency": "@(6,currency)",
                "accountType": "Cash Receipts"
              }
            }
          },
          "cashPayments": {
            "items": {
              "*": {
                "balance": "@(1,value)",
                "bankingDate": "@(2,toDate)",
                "accountName": "@(1,name)",
                "creationTime": "@(2,date)",
                "currency": "@(6,currency)",
                "accountType": "Cash Payments"
              }
            }
          }
        }
      }
    }
    },
  {
    "operation": "shift",
    "spec": {
      "reports": {
        "*": {
          "cashReceipts|cashPayments": {
            "items": {
              "*": "[]"
            }
          }
        }
      }
    }
    },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "name": "",
        "value": ""
      },
      "value": ""
    }
    }
]

我想导出最后一个项目。但有时它可能只出现在一个级别,有时它可能出现在 2 到 3 级。如果您看到“cashReceipts”:“cashPayments”项目之间的差异:数据意味着您可以理解差异。

需要迭代到我的项目是空的。但它可能会有所不同

标签: jsonjolt

解决方案


推荐阅读