需要JOLT规范文件来传输复杂的JSON

时间:2019-04-25 19:38:07

标签: jolt

我有一个复杂的JSON对象(在此示例中已将其简化),我无法弄清楚JOLT转换JSON的目的。有人对JOLT规范文件应该有什么想法吗?

原始JSON

[
  {
    "date": {
      "isoDate": "2019-03-22"
    },
    "application": {
      "name": "SiebelProject"
    },
    "applicationResults": [
      {
        "reference": {
          "name": "Number of Code Lines"
        },
        "result": {
          "value": 44501
        }
      },
      {
        "reference": {
          "name": "Transferability"
        },
        "result": {
          "grade": 3.1889542208002064
        }
      }
    ]
  },
  {
    "date": {
      "isoDate": "2019-03-21"
    },
    "application": {
      "name": "SiebelProject"
    },
    "applicationResults": [
      {
        "reference": {
          "name": "Number of Code Lines"
        },
        "result": {
          "value": 45000
        }
      },
      {
        "reference": {
          "name": "Transferability"
        },
        "result": {
          "grade": 3.8
        }
      }
    ]
  }
]

根据“名称” ASC,“日期” DESC进行转换和排序后所需的JSON

[
  {
    "Name": "SiebelProject",
    "Date": "2019-03-22",
    "Number of Code Lines": 44501,
    "Transferability" : 3.1889542208002064
  },
  {
    "Name": "SiebelProject",
    "Date": "2019-03-21",
    "Number of Code Lines": 45000,
    "Transferability" : 3.8
  }
]

1 个答案:

答案 0 :(得分:0)

我找不到一种进行排序的方法(我什至不确定您是否可以在JOLT中对降序进行排序),但这是进行转换的规范:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "date": {
          "isoDate": "[#3].Date"
        },
        "application": {
          "name": "[#3].Name"
        },
        "applicationResults": {
          "*": {
            "reference": {
              "name": {
                "Number of Code Lines": {
                  "@(3,result.value)": "[#7].Number of Code Lines"
                },
                "Transferability": {
                  "@(3,result.grade)": "[#7].Transferability"
                }
              }
            }
          }
        }
      }
    }
  }
]

之后,有些工具(例如jq)可以进行排序。