Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

parse json information in google script with no recurring first tag

In google sheet, I’ve got a script to retrieve JSon from an url

function getTeamData() {
  var DATA_SPREADSHEET_ID = "xxxx";
  var ss = SpreadsheetApp.openById(DATA_SPREADSHEET_ID);
  var TeamDataSheet = ss.getSheetByName("rawTeam");
  TeamDataSheet.clearContents();

  var url = 'xxxx';

  var response = UrlFetchApp.fetch(url);

  var content = response.getContentText();
  var data = JSON.parse(content);
  //Logger.log(JSON.stringify(data));

The problem I’ve got is that the JSon content look like this

{
    "03wi4": {
        "statsIncId": 5546,
        "rotowireId": 3795,
        "name": "Saunders, Wesley",
        "fantraxId": "03wi4",
        "team": "NY",
        "position": "SG"
    },
    "059gh": {
        "statsIncId": 6321,
        "rotowireId": 4919,
        "name": "Booth, Phil",
        "fantraxId": "059gh",
        "team": "(N/A)",
        "position": "PG"
    },
    "02nfe": {
        "statsIncId": 5020,
        "rotowireId": 3308,
        "sportRadarId": "c555e067-c4d5-43f6-99af-716b6005cbba",
        "name": "Henson, John",
        "fantraxId": "02nfe",
        "team": "NY",
        "position": "PF"
    },
    "033b7": {
        "statsIncId": 5235,
        "rotowireId": 3538,
        "name": "Raduljica, Miroslav",
        "fantraxId": "033b7",
        "team": "(N/A)",
        "position": "C"
    },
...
}

I would like to be able to return the team for each of the entries but I don’t have a common first tag name…
Could you please help, the result should look like this :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

059gh    (N/A)
02nfe    NY
033b7    (N/A)```

>Solution :

I believe your goal is as follows.

  • You want to retrieve the values like [["03wi4","NY"],["059gh","(N/A)"],["02nfe","NY"],["033b7","(N/A)"]] from your showing JSON object.
  • Your showing JSON object is data of var data = JSON.parse(content);.

In this case, how about the following modification?

Modified script:

function getTeamData() {
  var DATA_SPREADSHEET_ID = "xxxx";
  var ss = SpreadsheetApp.openById(DATA_SPREADSHEET_ID);
  var TeamDataSheet = ss.getSheetByName("rawTeam");
  TeamDataSheet.clearContents();

  var url = 'xxxx';
  var response = UrlFetchApp.fetch(url);
  var content = response.getContentText();
  var data = JSON.parse(content);

  // I added the below script.
  var res = Object.entries(data).map(([k, { team }]) => [k, team]);
  TeamDataSheet.getRange(1, 1, res.length, res[0].length).setValues(res);
}
  • When your previous question is considered, please modify the above script as follows.

    • From

        TeamDataSheet.getRange(1, 1, res.length, res[0].length).setValues(res);
      
    • To

        TeamDataSheet.getRange(1, 1, res.length, res[0].length).setNumberFormat("@").setValues(res);
      
  • Or,

    • From

        var res = Object.entries(data).map(([k, { team }]) => [k, team]);
        TeamDataSheet.getRange(1, 1, res.length, res[0].length).setValues(res);
      
    • To

        var res = Object.entries(data).map(([k, { team }]) => [`'${k}`, team]);
        TeamDataSheet.getRange(1, 1, res.length, res[0].length).setValues(res);
      

Note:

  • In this modification, it supposes that the value of data is your showing JSON object. Please be careful about this.

References:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading