1

I am working on developing an app for personal use that will pull characters from a game and put them in a google Spreadsheet. I am currently attempting this through google script.

The issue I am having is I am unable to find out how to parse through specific data sets without having to call the name specifically.

Here is the JSON output I am dealing with. The way it is structure it has a data set called "data" Inside of that it has all the characters with their own data sets. Within each one will have the specific character details. I need to find a way to parse through all the character names. In the example below it would be name1 and name2. There is about 100+ of these that I want to loop through and add the details to a google spreadsheet.

{
  "type": "character",
  "format": "standAloneComplex",
  "data": {
    "name1": {
      "id": "name1",
      "key": "1",
      "name": "name1",
      "info": {
        "attack": 8,
        "defense": 4,
        "magic": 3,
        "difficulty": 4
      },
      "tags": [
        "Fighter",
        "Tank"
      ]
    },
    "name2": {
      "version": "9.22.1",
      "id": "name2",
      "key": "2",
      "name": "name2",
      "info": {
        "attack": 3,
        "defense": 4,
        "magic": 8,
        "difficulty": 5
      },
      "tags": [
        "Mage",
        "Assassin"
      ]
    }

Inside Google scripts I am not sure how to call the data within the "data" section with a wild card or something to iterate. The only way I can get information to display is like so:

so I pull in the JSON using:

var characterJSON = UrlFetchApp.fetch("http://URLtoJSON.com");

Then I use the JSON.parse function to allow me to parse the data

var characterObject = JSON.parse(characterJSON);

In the examples I have found online you can just iterate through the data like so:

This example I will pull the "id" for the first character:

Logger.log(characterObject["data"][0]["id"]);

this works if the JSON had all the of the data enclosed in [] to make it an array. I cannot use the [0] option since it is an group of character objects within the "data" object. The only way I am able to access the data is if I call the name directly like so:

Logger.log(characterObject.data.name1.id);

Which would return name1. So I can pull things individually, but I want to be able to iterate through the data so I can enter it into the Google Spreadsheet.

Here is all the code together:

var characterJSON = UrlFetchApp.fetch("http://URLtoJSON.com");
var characterObject = JSON.parse(characterJSON);
Logger.log(characterObject["data"][0]["id"]);
Logger.log(characterObject.data.name1.id);
//or this method also works:
Logger.log(characterObject["data"]["name1"]["id"]);

0 Answers0