How to extract specific data from a JSON payload using SelectToken?

Hello.

I have a json and I can retrieve all the information except “DDD” and “numero”

Can someone explain to me why this method doesn’t work for this case?valoresApi.SelectToken(“telefone”)(“ddd”)

json chunk not captured

  "telefones": [
    {
      "ddd": "12",
      "numero": "ddddd"
    }
  ]

full json

loaded data{
  "ni": "",
  "tipoEstabelecimento": "1",
  "nomeEmpresarial": "",
  "nomeFantasia": "L",
  "situacaoCadastral": {
    "codigo": "2",
    "data": "20",
    "motivo": ""
  },
  "naturezaJuridica": {
    "codigo": "",
    "descricao": "Sociedade Empresária Limitada"
  },
  "dataAbertura": ",
  "cnaePrincipal": {
    "codigo": "",
    "descricao": "
  },
  "cnaeSecundarias": [
   
  ],
  "endereco": {
    "tipoLogradouro": "RUA",
    "logradouro": ",
    "numero": "",
    "complemento": "",
    "cep": 
    "bairro": "
    "municipio": {
      "codigo": 
      "descricao": 
    },
    "uf": "SP",
    "pais": {
      "codigo": "105",
      "descricao": "BRASIL"
    }
  },
  "municipioJurisdicao": {
    "codigo": "",
    "descricao": ""
  },
  "telefones": [
    {
      "ddd": "12",
      "numero": "ddddd"
    }
  ],
  "correioEletronico": "BR",
  "capitalSocial": 5,
  "porte": "01",
  "situacaoEspecial": "",
  "dataSituacaoEspecial": ""
}

Check the JSON value that you want to extract here: https://jsonformatter.org/json-parser

Example:

Thanks, I checked the link you sent.
I still can’t extract the desired value in Uipath

Based on the sample you provided, it’s not valid JSON as you have a couple unclosed double quotes and missing commas and/or extra colons.

I would correct these before trying to extract references out of it.
Can be validated with the site @marian.platonov mentioned, or others like https://jsonlint.com/ or any Linting commands/tools.

Error: Parse error on line 15:
...},	"dataAbertura": ",	"cnaePrincipal":
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'

Error: Parse error on line 18:
... "",		"descricao": "	},	"cnaeSecundar
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'

Error: Parse error on line 25:
...A",		"logradouro": ",		"numero": "",	
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'

Error: Parse error on line 28:
...",		"cep": "bairro": "		"municipio": {
----------------------^
Expecting 'EOF', '}', ',', ']', got ':'

Error: Parse error on line 28:
...",		"cep": "bairro": ""		"municipio": 
----------------------^
Expecting 'EOF', '}', ',', ']', got ':'

Error: Parse error on line 28:
...		"cep": "bairro"		"municipio": {			"c
----------------------^
Expecting 'EOF', '}', ':', ',', ']', got 'STRING'

Error: Parse error on line 30:
...codigo": "descricao":		},		"uf": "SP",
-----------------------^
Expecting 'EOF', '}', ',', ']', got ':'

Right,
The quotes were probably erased when deleting the data so it doesn’t show up in the post. My current problem is with the extraction of data from the phone number. So much so that the rest of the before and after information is captured correctly.

corret Json

It does not appear with error on the site mentioned by the friend @marian.platonov

{
  "ni": "",
  "tipoEstabelecimento": "1",
  "nomeEmpresarial": "",
  "nomeFantasia": "",
  "situacaoCadastral": {
    "codigo": "2",
    "data": "",
    "motivo": ""
  },
  "naturezaJuridica": {
    "codigo": "2062",
    "descricao": "Sociedade Empresária Limitada"
  },
  "dataAbertura": "",
  "cnaePrincipal": {
    "codigo": "4751201",
    "descricao": "Comércio varejista especializado de equipamentos e suprimentos de informática"
  },
   "endereco": {
    "tipoLogradouro": "RUA",
    "logradouro": "I",
    "numero": "115",
    "complemento": "",
    "cep": "",
    "bairro": "JARDIM SAO DIMAS",
    "municipio": {
      "codigo": "7099",
      "descricao": "SAO JOSE DOS CAMPOS"
    },
    "uf": "SP",
    "pais": {
      "codigo": "105",
      "descricao": "BRASIL"
    }
  },
  "municipioJurisdicao": {
    "codigo": "0812000",
    "descricao": "SÃO JOSÉ DOS CAMPOS"
  },
  "telefones": [
    {
      "ddd": "12",
      "numero": ""
    }
  ],
  "correioEletronico": "",
  "capitalSocial": "" ,
  "porte": "01",
  "situacaoEspecial": "",
  "dataSituacaoEspecial": ""
}

image

So I think you need to do something like this:

out_json_to_manipulate.SelectToken("telefones[0].ddd").ToString
out_json_to_manipulate.SelectToken("telefones[0].numero").ToString

Results:

image
image

Reference: https://docs.uipath.com/activities/docs/deserialize-json

1 Like

Marian beat me to an example, but I wanted to point out that you have a couple ways to query your data, which goes into more details with Newtonsoft’s Querying JSON with SelectToken which is

  • JToken Path as you are trying to do
  • JSONPath (Created to mimic XPath for XML) and the example I use below (JSONPath - XPath for JSON)
  • LINQ (Language Integrated Query)

image

To grab the first records ddd from the root

  • jsonObject.SelectToken("$.telefones[0].ddd")

Or say if you wanted to grab all defined ddd

  • jsonObject.SelectToken("..ddd")

image

Thanks, @marian.platonov
I’m not familiar with json, but I believe I understand why I can’t extract this data in the same way as others. I will study the link you gave me thanks

@codemonkee

Thanks for the explanation, I’ll study more about the information you gave me.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.