Search code examples
jsonjolt

I need to understand the JOLT functions like #, @, &, () and []


I'm having another problem, consider this JSON:

{
  "found": true,
  "consultado": "00000000",
  "nome": "EMPRESA DE CNPJ 00000000 LTDA",
  "socio": [
    {
      "cpfCnpj": "11111111111",
      "nome": "PESSOA DE CPF 11111111111",
      "descricaoCargo": "Sócio",
      "outrasEmpresasSocio": [
        {
          "cnpj": "11111111",
          "nome": "EMPRESA DE CNPJ 11111111 LTDA",
          "descricaoCargo": "Sócio-Administrador"
        }
      ]
    },
    {
      "cpfCnpj": "00000000000",
      "nome": "PESSOA DE CPF 00000000000",
      "descricaoCargo": "Sócio-Administrador",
      "outrasEmpresasSocio": [
        {
          "cnpj": "11111111",
          "nome": "EMPRESA DE CNPJ 11111111 LTDA",
          "descricaoCargo": "Sócio-Administrador"
        },
        {
          "cnpj": "22222222",
          "nome": "EMPRESA DE CNPJ 22222222 LTDA",
          "descricaoCargo": "Sócio-Administrador"
        },
        {
          "cnpj": "33333333",
          "nome": "EMPRESA DE CNPJ 33333333 LTDA",
          "descricaoCargo": "Sócio-Administrador"
        }
      ]
    }
  ]
}

How do I go through it and get each field socio.cpfCnpj, socio.nome, socio.descricaoCargo and each socio.outrasEmpresasSocio.cnpj, socio.outrasEmpresasSocio.nome and socio.outrasEmpresasSocio.descricaoCargo.

The result should be:

[
  {
    "cpfCnpj": "11111111111",
    "nome": "PESSOA DE CPF 11111111111",
    "cnpj": "11111111",
    "nome": "EMPRESA DE CNPJ 11111111 LTDA",
    "descricaoCargo": "Sócio-Administrador"
  },
  {
    "cpfCnpj": "00000000000",
    "nome": "PESSOA DE CPF 00000000000",
    "cnpj": "11111111",
    "nome": "EMPRESA DE CNPJ 11111111 LTDA",
    "descricaoCargo": "Sócio-Administrador"
  },
  {
    "cpfCnpj": "00000000000",
    "nome": "PESSOA DE CPF 00000000000",
    "cnpj": "22222222",
    "nome": "EMPRESA DE CNPJ 22222222 LTDA",
    "descricaoCargo": "Sócio-Administrador"
  },
  {
    "cpfCnpj": "00000000000",
    "nome": "PESSOA DE CPF 00000000000",
    "cnpj": "33333333",
    "nome": "EMPRESA DE CNPJ 33333333 LTDA",
    "descricaoCargo": "Sócio-Administrador"
  }
]

I still get lost trying to understand the #, @, &, () and [] functions in JOLT.

Could you help me with the problem and explain these functions to me, please?


Solution

  • What you need is to loop within the objects of the "outrasEmpresasSocio" arrays while bringing two attributes cpfCnpj and nome from two levels upper level( by using such as @2,cpfCnpj ), and separate the levels by using the indexes of the arrays "socio" and "outrasEmpresasSocio" for which going 1 and 3 levels up the tree would be needed, and combining them I got the common identifier &3_&1 such as

    [
      {
        "operation": "shift",
        "spec": {
          "socio": {
            "*": {
              "outrasEmpresasSocio": {
                "*": {
                  "@2,cpfCnpj": "&3_&1.cpfCnpj",
                  "@2,nome": "&3_&1.nomeOut",
                  "*": "&3_&1.&" // asterisk( "*" ) represents all elements within 
                                 // the current respective object
                                 // & which stays at the rightmost represents
                                 // the leaf node to replicate the value of the current 
                                 // attribute, indeed & is identical to &0 where
                                 // zero means the current level
                }
              }
            }
          }
        }
      },
      { // get rid of the object keys
        "operation": "shift",
        "spec": {
          "*": "[]"
        }
      }
    ]