Data from a file. Json

Asked

Viewed 58 times

0

Eai guys, all right? ... I’m having a problem where I need to get a data block from a json file, being it:

{
        "custom_fields": {
            "[TS] A empresa em que você trabalha é": "uma fábrica/indústria",
            "[TS] Setor da empresa": "Outros",
            "[TS] Compra de Lubrificante": "Sim - acima de 2.000 litros",
            "[TS] Função na empresa": "Técnico",
            "[TS] Departamento da empresa": "Administração",
            "[TS] Site": "SCP"
        }
}

I’m trying a big problem to get the data where has bracket and space, because always gives the error: Syntaxerror: Unexpected token [

var JSONSource = JSON.stringify(requestData);

  //Extrai dados do lead para inserção
  for (var i = 0; i < leadData.length; i++) {
    values.push([
                 JSONSource,
                 leadData[i].custom_fields.[TS] A empresa em que você trabalha é,
                 leadData[i].custom_fields.[TS] Setor da empresa,
                 leadData[i].[TS] Compra de Lubrificante,
                 leadData[i].[TS] Departamento da empresa,
                 leadData[i].[TS] Site
                ]);
  }

Finally, I wanted to know how to get this same data with this formatting, and this formatting/ structure is coming from an API, so I can’t change the key of it.

Another thing is that I’m doing this in the google Sheets script, which uses the same engine as Js, but with its peculiarities, I’m having some difficulty because I have almost no knowledge in this type of script.

From now on I appreciate your help!

1 answer

1


Knowing the keys, try to access as an array. See below:

leadData[i].custom_fields["[TS] A empresa em que você trabalha é"];

It is necessary to access this way because the keys are irregular (have space, accents, etc). If there is a key that is just a word, no special characters, you will be able to access with dot leadData[i].custom_fields.exemplo1.

Hugs

  • Thanks Tiago, I got it here. But my friend, because hell I can access some data leadData[i]. custom_fields.exemplo1 and this one does not, both of which have the same structure?

  • 1

    @Moisesfausto I increased the answer with this your doubt.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.