使用嵌套数组进行Jolt转换。

时间:2016-05-25 06:53:48

标签: jolt

我正在尝试编写一个规范,使用jolt变换进行下面的转换。我只对更改json中键的名称感兴趣,值应该保持不变。帮帮我。

输入Json:

[
  {
    "list1": [
      {
        "id": "CPP1600000009846",
        "list2": [
          {
            "amount": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "code": "CONAMP"
          },
          {
            "amount": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "code": "PCCPRI"
          },
          {
            "amount": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "code": "PCCPCI"
          },
          {
            "amount": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "code": "PCCPII"
          }
        ]
      },
      {
        "id": "CPP1600000009846",
        "list2": [
          {
            "amount": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "code": "CONEIT"
          },
          {
            "amount": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "code": "CONCRT"
          },
          {
            "amount": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "code": "CONNCT"
          }
        ]
      }
    ]
  }
]

预期产出:

[
  {
    "listA": [
      {
        "Num": "CPP1600000009846",
        "listB": [
          {
            "rate": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "covg_code": "CONAMP"
          },
          {
            "rate": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "covg_code": "PCCPRI"
          },
          {
            "rate": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "covg_code": "PCCPCI"
          },
          {
            "rate": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "covg_code": "PCCPII"
          }
        ]
      },
      {
        "Num": "CPP1600000009846",
        "listB": [
          {
            "rate": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "covg_code": "CONEIT"
          },
          {
            "rate": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "covg_code": "CONCRT"
          },
          {
            "rate": {
              "formattedPrimeAmount": "0.00",
              "primeAmount": "0.00"
            },
            "covg_code": "CONNCT"
          }
        ]
      }
    ]
  }
]

2 个答案:

答案 0 :(得分:0)

使用以下规范

[
  {
    "operation": "shift",
    "spec": {
        "list1" : {
            "*": {
                "id" : "listA[&1].Num",
                "list2" : {
                    "*": {
                        "amount" : "listA[&1].listB[&3].rate",
                        "code" : "listA[&1].listB[&3].covg_code"
                    }
                }
            }
        }
    }
  }

]

使用以下代码运行此规范

    List<Object> chainrSpecJSON = JsonUtils.filepathToList( "D:\\path\\to\\spec.json" );
    Chainr chainr = Chainr.fromSpec( chainrSpecJSON );

    List<Object> inputJSONList = JsonUtils.filepathToList( "D:\\path\\to\\input.json" );

    List<Object> outputList = new ArrayList<Object>();

    for(Object singleObj : inputJSONList){
        Object transformedOutput = chainr.transform( singleObj );
        outputList.add(transformedOutput);
    }

答案 1 :(得分:-1)

对于迟到的重播感到抱歉,请使用以下规格

text = status.text.encode('utf8')