简体   繁体   中英

Parse the following JSON using google apps script

-- Hi everyone, It's been now several days I'm trying to parse the following JSON using google apps script.

[
{
"NOMBRE": "ViejosNoUsarEl Quebrachal",
"ACTIVO": false,
"CODIGO": "ViejosNoUsarQUEB",
"CALLE": null,
"NUMERO": null,
"PROVINCIA": "Jujuy",
"LOCALIDAD": "EL MORRO",
"ZONA": null,
"SUPERFICIE": 3900,
"CODIGOEXTERNO": ""
},
{
"NOMBRE": "ViejoNoUsarSanta Teresa",
"ACTIVO": false,
"CODIGO": "ViejoNoUsarST",
"CALLE": null,
"NUMERO": null,
"PROVINCIA": "San Luis",
"LOCALIDAD": "Villa MercedesOLD",
"ZONA": "Oeste",
"SUPERFICIE": 3700,
"CODIGOEXTERNO": ""
},
{
"NOMBRE": "ViejosNoUsarGil",
"ACTIVO": false,
"CODIGO": "ViejosNoUsarGIL",
"CALLE": null,
"NUMERO": null,
"PROVINCIA": "Cordoba",
"LOCALIDAD": "9 DE JULIO",
"ZONA": "Oeste",
"SUPERFICIE": 200,
"CODIGOEXTERNO": ""
},
{
"NOMBRE": "ViejosNoUsarDon Manuel",
"ACTIVO": false,
"CODIGO": "ViejosNoUsarDM",
"CALLE": null,
"NUMERO": null,
"PROVINCIA": "Cordoba",
"LOCALIDAD": "9 DE JULIO",
"ZONA": "Oeste",
"SUPERFICIE": 400,
"CODIGOEXTERNO": ""
}
]

The GET response is giving me the JSON as I posted it.

Using google apps script I want to add on a google sheet as much rows as objects are in the array.

In this case there would be 4 google sheet rows. I want to parse only the values of the properties.

As an example, the first row would look like this:

ViejosNoUsarEl Quebrachal | false | ViejosNoUsarQUEB | null | null | Jujuy | EL MORRO | null | 3900 |

I want to focus on this question on the pasrsing matter, not on the adding the rows to the google sheet yet.

The problem is that I cant get the dot notation to extract the values I want.

For example, Logger.log(response.provincia); prints "Information null".

Modification points:

  • From your showing sample data and For example, Logger.log(response.provincia); prints "Information null". For example, Logger.log(response.provincia); prints "Information null". , I thought that the reason for your issue is due to that you are trying to retrieve the values from an array using response.provincia . In this case, it is required to be response[i].PROVINCIA . i is the index of an array. If you want to retrieve the value of "PROVINCIA" of the 1st element of the array, you can use response[0].PROVINCIA . From your showing data, provincia is required to be PROVINCIA . When response[0].provincia is run, undefined is returned. Please be careful about this.
  • When you want to retrieve the values like ViejosNoUsarEl Quebrachal | false | ViejosNoUsarQUEB | null | null | Jujuy | EL MORRO | null | 3900 | ViejosNoUsarEl Quebrachal | false | ViejosNoUsarQUEB | null | null | Jujuy | EL MORRO | null | 3900 | in order, in this case, the values are retrieved by preparing the keys in order.

When these points are reflected in a sample script, it becomes as follows.

Sample script:

 const keys = ["NOMBRE", "ACTIVO", "CODIGO", "CALLE", "NUMERO", "PROVINCIA", "LOCALIDAD", "ZONA", "SUPERFICIE", "CODIGOEXTERNO"]; const response = [ { "NOMBRE": "ViejosNoUsarEl Quebrachal", "ACTIVO": false, "CODIGO": "ViejosNoUsarQUEB", "CALLE": null, "NUMERO": null, "PROVINCIA": "Jujuy", "LOCALIDAD": "EL MORRO", "ZONA": null, "SUPERFICIE": 3900, "CODIGOEXTERNO": "" }, { "NOMBRE": "ViejoNoUsarSanta Teresa", "ACTIVO": false, "CODIGO": "ViejoNoUsarST", "CALLE": null, "NUMERO": null, "PROVINCIA": "San Luis", "LOCALIDAD": "Villa MercedesOLD", "ZONA": "Oeste", "SUPERFICIE": 3700, "CODIGOEXTERNO": "" }, { "NOMBRE": "ViejosNoUsarGil", "ACTIVO": false, "CODIGO": "ViejosNoUsarGIL", "CALLE": null, "NUMERO": null, "PROVINCIA": "Cordoba", "LOCALIDAD": "9 DE JULIO", "ZONA": "Oeste", "SUPERFICIE": 200, "CODIGOEXTERNO": "" }, { "NOMBRE": "ViejosNoUsarDon Manuel", "ACTIVO": false, "CODIGO": "ViejosNoUsarDM", "CALLE": null, "NUMERO": null, "PROVINCIA": "Cordoba", "LOCALIDAD": "9 DE JULIO", "ZONA": "Oeste", "SUPERFICIE": 400, "CODIGOEXTERNO": "" } ]; const values = response.map(o => keys.map(h => o[h])); console.log(values)

  • When this script is run, the values are returned as the 2-dimensional array. This can be used for putting to the Spreadsheet using setValues .

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM