使用JOLT将列表中的属性名称更改为列出嵌套json

6ovsh4lw  于 2022-12-15  发布在  其他
关注(0)|答案(3)|浏览(161)

我是json转换的新手,所以我想找一些帮助来把有值列表的嵌套json转换成有列表的嵌套json。

输入json

{
  "userData": [
    {
      "userId": "1",
      "age": "20",
      "firstName": "firstname1",
      "lastname": "lastname1",
      "zipCode": "zipcode1",
      "street": "street1",
      "city": "city1",
      "country": "country",
      "gender": "gender1",
      "grade": "grade1",
      "birthday": "birthday1"
    },
    {
      "userId": "2",
      "age": "25",
      "firstName": "firstname2",
      "lastname": "lastname2",
      "zipCode": "zipcode2",
      "street": "street2",
      "city": "city2",
      "country": "country2",
      "gender": "gender2",
      "grade": "grade2",
      "birthday": "birthday2"
    }
  ]
}

震动规格

[
  {
    "operation": "shift",
    "spec": {
      "userData": {
        "*": {
          "userId": "data.[&1].ID",
          "*": "data.[&1].&",
          "zipCode": {
            "#custom-field1": "adr_&1.property",
            "@": "adr_&.value"
          },
          "street": {
            "#custom-field2": "adr_&1.property",
            "@": "adr_&.value"
          },
          "city": {
            "#custom-field3": "adr_&1.property",
            "@": "adr_&.value"
          },
          "country": {
            "#custom-field4": "adr_&1.property",
            "@": "adr_&.value"
          }
        }
      }
    }
  }
]

产出

{
  "data" : [ {
    "ID" : "1",
    "age" : "20",
    "firstName" : "firstname1",
    "lastname" : "lastname1",
    "gender" : "gender1",
    "grade" : "grade1",
    "birthday" : "birthday1"
  }, {
    "ID" : "2",
    "age" : "25",
    "firstName" : "firstname2",
    "lastname" : "lastname2",
    "gender" : "gender2",
    "grade" : "grade2",
    "birthday" : "birthday2"
  } ],
  "adr_zipCode" : {
    "property" : [ "custom-field1", "custom-field1" ],
    "value" : [ "zipcode1", "zipcode2" ]
  },
  "adr_street" : {
    "property" : [ "custom-field2", "custom-field2" ],
    "value" : [ "street1", "street2" ]
  },
  "adr_city" : {
    "property" : [ "custom-field3", "custom-field3" ],
    "value" : [ "city1", "city2" ]
  },
  "adr_country" : {
    "property" : [ "custom-field4", "custom-field4" ],
    "value" : [ "country", "country2" ]
  }
}

需要json

{
  "data": [
    {
      "ID": "1",
      "age": "20",
      "firstName": "firstname1",
      "lastname": "lastname1",
      "gender": "gender1",
      "grade": "grade1",
      "birthday": "birthday1",
      "address": [
        {
          "property": "custom-field1",
          "value": "zipcode1"
        },
        {
          "property": "custom-field2",
          "value": "street1"
        },
        {
          "property": "custom-field3",
          "value": "city1"
        },
        {
          "property": "custom-field4",
          "value": "country"
        }
      ]
    },
    {
      "ID": "2",
      "age": "25",
      "firstName": "firstname2",
      "lastname": "lastname2",
      "gender": "gender2",
      "grade": "grade2",
      "birthday": "birthday2",
      "address": [
        {
          "property": "custom-field1",
          "value": "zipcode2"
        },
        {
          "property": "custom-field2",
          "value": "street2"
        },
        {
          "property": "custom-field3",
          "value": "city2"
        },
        {
          "property": "custom-field4",
          "value": "country2"
        }
      ]
    }
  ]
}
2uluyalo

2uluyalo1#

您可以使用索引的单个value数组,这些数组将在即将发布的规范中使用,并带有一个棘手的第零个成员,该成员在使用后将被删除,例如

[
  {
    "operation": "shift",
    "spec": {
      "userData": {
        "*": {
          "userId": "data[&1].ID",
          "@(0,age)": "data[&1].age", //multiplexed this attribute in order to make it the zeroth index, which will be removed within the upcoming specs, for the "address" array at the same time, as indexing starts from zero ...
          "*": "data[&1].&",
          "a*|z*|s*|c*": {//separating "zipCode", "street", "city", "country" attributes from the others while using "a*" for "age" is tricky, temporarily
            "@": "data[&2].value"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "*": "&2[&1].&",
          "value": {
            "*": {
              "#property": "&4[&3].address[&1].custom-field&1",
              "@(2,value[&])": "&4[&3].address[&1].value"
            }
          }
        }
      }
    }
  },
  { // the attributes are written individually to keep the desired sorting
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "ID": "&2[&1].&",
          "age": "&2[&1].&",
          "firstName": "&2[&1].&",
          "lastname": "&2[&1].&",
          "gender": "&2[&1].&",
          "grade": "&2[&1].&",
          "birthday": "&2[&1].&",
          "address": {
            "0": "&4[&3].&2[&1]",
            "*": {
              "*": {
                "$": "&5[&4].&3[&2].@(0)" // key-value pairs are exchanged for this attribute
              },
              "value": "&4[&3].&2[&1].&"
            }
          }
        }
      }
    }
  },
  { // get rid of null components of the array
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  }
]
xmjla07d

xmjla07d2#

[
    {
        "operation": "shift",
        "spec": {
            "userData": {
                "*": {
                    "userId": "data.[&1].ID",
                    "*": "data.[&1].&",
                    "zipCode": {
                        "#custom-field1": "data[#3].address[#2].property",
                        "@": "data[#3].address[#2].value"
                    },
                    "street": {
                        "#custom-field2": "data[#3].address[#2].property",
                        "@": "data[#3].address[#2].value"
                    },
                    "city": {
                        "#custom-field3": "data[#3].address[#2].property",
                        "@": "data[#3].address[#2].value"
                    },
                    "country": {
                        "#custom-field4": "data[#3].address[#2].property",
                        "@": "data[#3].address[#2].value"
                    }
                }
            }
        }
    },
    { // get rid of null components of the array
        "operation": "modify-overwrite-beta",
        "spec": {
            "*": "=recursivelySquashNulls"
        }
    }
]
qlfbtfca

qlfbtfca3#

您可以考虑使用另一个库 Josson 通过更短的转换语句来完成相同的工作。
https://github.com/octomix/josson

荒漠化

Josson josson = Josson.fromJsonString(
    "{" +
    "  \"userData\": [" +
    "    {" +
    "      \"userId\": \"1\"," +
    "      \"age\": \"20\"," +
    "      \"firstName\": \"firstname1\"," +
    "      \"lastname\": \"lastname1\"," +
    "      \"zipCode\": \"zipcode1\"," +
    "      \"street\": \"street1\"," +
    "      \"city\": \"city1\"," +
    "      \"country\": \"country\"," +
    "      \"gender\": \"gender1\"," +
    "      \"grade\": \"grade1\"," +
    "      \"birthday\": \"birthday1\"" +
    "    }," +
    "    {" +
    "      \"userId\": \"2\"," +
    "      \"age\": \"25\"," +
    "      \"firstName\": \"firstname2\"," +
    "      \"lastname\": \"lastname2\"," +
    "      \"zipCode\": \"zipcode2\"," +
    "      \"street\": \"street2\"," +
    "      \"city\": \"city2\"," +
    "      \"country\": \"country2\"," +
    "      \"gender\": \"gender2\"," +
    "      \"grade\": \"grade2\"," +
    "      \"birthday\": \"birthday2\"" +
    "    }" +
    "  ]" +
    "}");

转型

JsonNode node = josson.getNode(
    "map(data:userData" +
    "      .field(ID:userId, userId:," +
    "             zipCode:, street:, city:, country:," +
    "             address:collect(" +
    "               map(property:'customer-field1',value:zipCode)," +
    "               map(property:'customer-field2',value:street)," +
    "               map(property:'customer-field3',value:city)," +
    "               map(property:'customer-field4',value:country))))");
System.out.print(node.toPrettyString());

产出

{
  "data" : [ {
    "age" : "20",
    "firstName" : "firstname1",
    "lastname" : "lastname1",
    "gender" : "gender1",
    "grade" : "grade1",
    "birthday" : "birthday1",
    "ID" : "1",
    "address" : [ {
      "property" : "customer-field1",
      "value" : "zipcode1"
    }, {
      "property" : "customer-field2",
      "value" : "street1"
    }, {
      "property" : "customer-field3",
      "value" : "city1"
    }, {
      "property" : "customer-field4",
      "value" : "country"
    } ]
  }, {
    "age" : "25",
    "firstName" : "firstname2",
    "lastname" : "lastname2",
    "gender" : "gender2",
    "grade" : "grade2",
    "birthday" : "birthday2",
    "ID" : "2",
    "address" : [ {
      "property" : "customer-field1",
      "value" : "zipcode2"
    }, {
      "property" : "customer-field2",
      "value" : "street2"
    }, {
      "property" : "customer-field3",
      "value" : "city2"
    }, {
      "property" : "customer-field4",
      "value" : "country2"
    } ]
  } ]
}

相关问题