json 重构JOLT代码以获得预期的输出

b1payxdu  于 2023-10-21  发布在  其他
关注(0)|答案(1)|浏览(116)

输入JSON:

[
  {
    "id": "05301",
    "nome": "Região Metropolitana de Sorocaba",
    "UF": {
      "id": 35,
      "sigla": "SP",
      "nome": "São Paulo",
      "regiao": {
        "id": 3,
        "sigla": "SE",
        "nome": "Sudeste"
      }
    },
    "sub-regioes-metropolitanas": [
      {
        "id": "0530101",
        "nome": "Sub-região 1",
        "municipios": [
          {
            "id": 3500758,
            "nome": "Alambari"
          },
          {
            "id": 3507001,
            "nome": "Boituva"
          },
          {
            "id": 3510302,
            "nome": "Capela do Alto"
          },
          {
            "id": 3511508,
            "nome": "Cerquilho"
          },
          {
            "id": 3511607,
            "nome": "Cesário Lange"
          },
          {
            "id": 3522307,
            "nome": "Itapetininga"
          },
          {
            "id": 3525854,
            "nome": "Jumirim"
          },
          {
            "id": 3551108,
            "nome": "Sarapuí"
          },
          {
            "id": 3554003,
            "nome": "Tatuí"
          },
          {
            "id": 3554508,
            "nome": "Tietê"
          }
        ]
      }]},
  {
    "id": "05101",
    "nome": "Região Metropolitana de Campinas",
    "UF": {
      "id": 35,
      "sigla": "SP",
      "nome": "São Paulo",
      "regiao": {
        "id": 3,
        "sigla": "SE",
        "nome": "Sudeste"
      }
    },
    "sub-regioes-metropolitanas": [],
    "municipios": [
      {
        "id": 3501608,
        "nome": "Americana"
      },
      {
        "id": 3503802,
        "nome": "Artur Nogueira"
      }
    ]
  }
]

我需要这个输出:

[ [ {
  "id_rgia_mptn" : "05301",
  "nome_rgia_mptn" : "Região Metropolitana de Sorocaba",
  "id_sub_rgia_mptn" : "0530101",
  "nome_sub_rgia_mptn" : "Sub-região 1",
  "id_mcpo" : 3500758,
  "id_uf" : 35,
  "sgla_uf" : "SP",
  "id_rgia" : 3,
  "sgla_rgia" : "SE"
},
{
  "id_rgia_mptn" : "05301",
  "nome_rgia_mptn" : "Região Metropolitana de Sorocaba",
  "id_sub_rgia_mptn" : "0530101",
  "nome_sub_rgia_mptn" : "Sub-região 1",
  "id_mcpo" : 3507001,
  "id_uf" : 35,
  "sgla_uf" : "SP",
  "id_rgia" : 3,
  "sgla_rgia" : "SE"
},
{
  "id_rgia_mptn" : "05301",
  "nome_rgia_mptn" : "Região Metropolitana de Sorocaba",
  "id_sub_rgia_mptn" : "0530101",
  "nome_sub_rgia_mptn" : "Sub-região 1",
  "id_mcpo" : 3510302,
  "id_uf" : 35,
  "sgla_uf" : "SP",
  "id_rgia" : 3,
  "sgla_rgia" : "SE"
},
{
  "id_rgia_mptn" : "05301",
  "nome_rgia_mptn" : "Região Metropolitana de Sorocaba",
  "id_sub_rgia_mptn" : "0530101",
  "nome_sub_rgia_mptn" : "Sub-região 1",
  "id_mcpo" : 3511508,
  "id_uf" : 35,
  "sgla_uf" : "SP",
  "id_rgia" : 3,
  "sgla_rgia" : "SE"
},
{
  "id_rgia_mptn" : "05301",
  "nome_rgia_mptn" : "Região Metropolitana de Sorocaba",
  "id_sub_rgia_mptn" : "0530101",
  "nome_sub_rgia_mptn" : "Sub-região 1",
  "id_mcpo" : 3511607,
  "id_uf" : 35,
  "sgla_uf" : "SP",
  "id_rgia" : 3,
  "sgla_rgia" : "SE"
},
{
  "id_rgia_mptn" : "05301",
  "nome_rgia_mptn" : "Região Metropolitana de Sorocaba",
  "id_sub_rgia_mptn" : "0530101",
  "nome_sub_rgia_mptn" : "Sub-região 1",
  "id_mcpo" : 3522307,
  "id_uf" : 35,
  "sgla_uf" : "SP",
  "id_rgia" : 3,
  "sgla_rgia" : "SE"
},
{
  "id_rgia_mptn" : "05301",
  "nome_rgia_mptn" : "Região Metropolitana de Sorocaba",
  "id_sub_rgia_mptn" : "0530101",
  "nome_sub_rgia_mptn" : "Sub-região 1",
  "id_mcpo" : 3525854,
  "id_uf" : 35,
  "sgla_uf" : "SP",
  "id_rgia" : 3,
  "sgla_rgia" : "SE"
},
{
  "id_rgia_mptn" : "05301",
  "nome_rgia_mptn" : "Região Metropolitana de Sorocaba",
  "id_sub_rgia_mptn" : "0530101",
  "nome_sub_rgia_mptn" : "Sub-região 1",
  "id_mcpo" : 3551108,
  "id_uf" : 35,
  "sgla_uf" : "SP",
  "id_rgia" : 3,
  "sgla_rgia" : "SE"
},
{
  "id_rgia_mptn" : "05301",
  "nome_rgia_mptn" : "Região Metropolitana de Sorocaba",
  "id_sub_rgia_mptn" : "0530101",
  "nome_sub_rgia_mptn" : "Sub-região 1",
  "id_mcpo" : 3554003,
  "id_uf" : 35,
  "sgla_uf" : "SP",
  "id_rgia" : 3,
  "sgla_rgia" : "SE"
},
{
  "id_rgia_mptn" : "05301",
  "nome_rgia_mptn" : "Região Metropolitana de Sorocaba",
  "id_sub_rgia_mptn" : "0530101",
  "nome_sub_rgia_mptn" : "Sub-região 1",
  "id_mcpo" : 3554508,
  "id_uf" : 35,
  "sgla_uf" : "SP",
  "id_rgia" : 3,
  "sgla_rgia" : "SE"
},
{
  "id_rgia_mptn" : "05101",
  "nome_rgia_mptn" : "Região Metropolitana de Campinas",
  "id_sub_rgia_mptn" : "",
  "nome_sub_rgia_mptn" : "",
  "id_mcpo" : 3501608,
  "id_uf" : 35,
  "sgla_uf" : "SP",
  "id_rgia" : 3,
  "sgla_rgia" : "SE"
},
{
  "id_rgia_mptn" : "05101",
  "nome_rgia_mptn" : "Região Metropolitana de Campinas",
  "id_sub_rgia_mptn" : "",
  "nome_sub_rgia_mptn" : "",
  "id_mcpo" : 3503802,
  "id_uf" : 35,
  "sgla_uf" : "SP",
  "id_rgia" : 3,
  "sgla_rgia" : "SE"
}]]

我尝试了这个JOLT,我没有得到预期的输出:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "id": "[&1].id_rgia_mptn",
        "nome": "[&1].nome_rgia_mptn",
        "sub-regioes-metropolitanas": {
          "*": {
            "id": "[&1].id_sub_rgia_mptn",
            "nome": "[&1].nome_sub_rgia_mptn",
            "municipios": {
              "*": {
                "id": "[&3].id_mcpo"
              }
            }
          }
        },
        "UF": {
          "id": "[&2].id_uf",
          "sigla": "[&2].sgla_uf",
          "regiao": {
            "id": "[&3].id_rgia",
            "sigla": "[&3].sgla_rgia"
          }
        }
      }
    }
  }
]
7z5jn7bk

7z5jn7bk1#

您应该添加更多的标识符,这些标识符将表示不同级别的相应数组的索引,以便在使用不同的标记(othersMain)标记每个组后动态处理问题时进行更多分区,例如

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&1.others.&_rgia_mptn",
        "municipios": {
          "*": {
            "id": "&3.Main[&1].&_mcpo"
          }
        },
        "sub-regioes-metropolitanas": {
          "*": {
            "*": "&1.others.&_sub_rgia_mptn",
            "municipios": {
              "*": {
                "id": "&3.Main[&1].id_mcpo"
              }
            }
          }
        },
        "UF": {
          "id": "&2.others.&_uf",
          "*i*": "&2.others.&(0,1)&(0,2)_uf",
          "*e*o": {
            "id": "&3.others.&_&(1,1)&(1,2)",
            "*i*": "&3.others.&(0,1)&(0,2)_&(1,1)&(1,2)"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "Main": {
          "*": {
            "@2,others": {
              "*": "&4_&2.&"
            }, // go two levels up the tree to grab the values of the "others" array
            "*": "&3_&1.&"
          }
        }
      }
    }
  },
  { // get rid of the object keys
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

相关问题