Search code examples
jsonwildcardjolt

I need to understand JOLT wildcards and do the correct JOLT here


I've this JSON

{
  "found": true,
  "consultado": "00000000",
  "nome": "EMPRESA DE CNPJ 00000000 LTDA",
  "participacaoEmpresa": [
    {
      "cnpj": "44444444",
      "nome": "EMPRESA DE CNPJ 44444444 LTDA",
      "descricaoCargo": "Sócio"
    },
    {
      "cnpj": "123",
      "nome": "EMPRESA DE CNPJ 123 LTDA",
      "descricaoCargo": "Sócio123"
    }
  ]
}

I've maded this JOLT

[
  {
    "operation": "shift",
    "spec": {
      "consultado": "DOCT_SCIO",
      "nome": "NOME_SCIO",
      "participacaoEmpresa": {
        "*": {
          "cnpj": "CNPJ_PSSA_JUDC",
          "nome": "NOME_PSSA_JUDC",
          "descricaoCargo": "DCRC_CRGO"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*_PSSA*|DCRC_CRGO": {
        "*": {
          "@": "[&1].&2"
        }
      }
    }
  }
]

But I've this output

[ {
  "DCRC_CRGO" : "Sócio",
  "CNPJ_PSSA_JUDC" : "44444444",
  "NOME_PSSA_JUDC" : "EMPRESA DE CNPJ 44444444 LTDA"
}, {
  "DCRC_CRGO" : "Sócio123",
  "CNPJ_PSSA_JUDC" : "123",
  "NOME_PSSA_JUDC" : "EMPRESA DE CNPJ 123 LTDA"
} ]

But I need this

[ {
  "DOCT_SCIO": "00000000",
  "NOME_SCIO": "EMPRESA DE CNPJ 00000000 LTDA",
  "DCRC_CRGO" : "Sócio",
  "CNPJ_PSSA_JUDC" : "44444444",
  "NOME_PSSA_JUDC" : "EMPRESA DE CNPJ 44444444 LTDA"
}, {
  "DOCT_SCIO": "00000000",
  "NOME_SCIO": "EMPRESA DE CNPJ 00000000 LTDA",
  "DCRC_CRGO" : "Sócio123",
  "CNPJ_PSSA_JUDC" : "123",
  "NOME_PSSA_JUDC" : "EMPRESA DE CNPJ 123 LTDA"
} ]

What I've maded wrong here and I need to change?

If you could help me, do a pass by pass or explain details, please.

I'm having problem to understand my mistakes and I've tryied much codes to get the needed output.

The JOLT documentation is confused.

Attached at question


Solution

  • Need to transfer the values DOCT_SCIO and NOME_SCIO into the innermost part of the JSON from the two upper levels, so use @2,DOCT_SCIO and @2,NOME_SCIO as in the following case :

    [
      {
        "operation": "shift",
        "spec": {
          "consultado": "DOCT_SCIO",
          "nome": "NOME_SCIO",
          "participacaoEmpresa": {
            "*": {
              "cnpj": "CNPJ_PSSA_JUDC",
              "nome": "NOME_PSSA_JUDC",
              "descricaoCargo": "DCRC_CRGO"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*_PSSA*|DCRC_CRGO": {
            "*": {
              "@2,DOCT_SCIO": "[&1].DOCT_SCIO",
              "@2,NOME_SCIO": "[&1].NOME_SCIO",
              "@": "[&1].&2"
            }
          }
        }
      },
      { // get rid of the repeated identical components to one per each element
        "operation": "cardinality",
        "spec": {
          "*": {
            "*": "ONE"
          }
        }
      }
    ]