qeet / IMPORTJSONAPI

Use JSONPath to selectively extract data from any JSON or GraphQL API directly into Google Sheets.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

calling the API key from within the script

keanumelody opened this issue · comments

Hello

I have added a few lines of code to the original ImportJson script which calls the API key from withing the script and not from a cell everytime you run the function from a cell in google sheets. Can you do that in your script? The lines of my code is:

function ImportJSON_mycustomapi(url, api_key, query, parseOptions) {
var header = {
headers: {
'key_token_myapi': 'myapikey',
'Accept': 'application/json'
}
}
return ImportJSONAdvanced(url, header, query, parseOptions, includeXPath_, defaultTransform_)
}

commented

Something like this should work:

function IMPORTJSONAPI_MyCustomApi(url, query, cols) {
     var headers = {
         'key_token_myapi': 'myapikey',
         'Accept': 'application/json'
    }
    return IMPORTJSONAPI(url, query, cols, "headers=" + JSON.stringify(headers)) 
}

OK thanks, I will try that, but the code does not allow me to run it properly anyway because it always demands query and cols. I have tried to read your instruction a couple of times but do not get it: how exactly should I use query and cols? I have an API request which imports data into a sheet and one of the columns is: "Response Car Type" and the other one is "Date and Time". I have tried to put them into the formula but with no success, your formula returns error to me either by query or cols. How can I put them into the formula exactly? Appreciate your help.

commented

If you post an example of the JSON response from the API then I can tell you what need to set query & cols to.

Please see the an example of the json response, I need for example name of the teams and half time score:

{
"get": "fixtures",
"parameters": {
"live": "all"
},
"errors": [],
"results": 4,
"paging": {
"current": 1,
"total": 1
},
"response": [
{
"fixture": {
"id": 239625,
"referee": null,
"timezone": "UTC",
"date": "2020-02-06T14:00:00+00:00",
"timestamp": 1580997600,
"periods": {
"first": 1580997600,
"second": null
},
"venue": {
"id": 1887,
"name": "Stade Municipal",
"city": "Oued Zem"
},
"status": {
"long": "Halftime",
"short": "HT",
"elapsed": 45
}
},
"league": {
"id": 200,
"name": "Botola Pro",
"country": "Morocco",
"logo": "https://media.api-sports.io/football/leagues/115.png",
"flag": "https://media.api-sports.io/flags/ma.svg",
"season": 2019,
"round": "Regular Season - 14"
},
"teams": {
"home": {
"id": 967,
"name": "Rapide Oued ZEM",
"logo": "https://media.api-sports.io/football/teams/967.png",
"winner": false
},
"away": {
"id": 968,
"name": "Wydad AC",
"logo": "https://media.api-sports.io/football/teams/968.png",
"winner": true
}
},
"goals": {
"home": 0,
"away": 1
},
"score": {
"halftime": {
"home": 0,
"away": 1
},
"fulltime": {
"home": null,
"away": null
},
"extratime": {
"home": null,
"away": null
},
"penalty": {
"home": null,
"away": null
}
}
}
]
}

{ "get": "fixtures", "parameters": { "live": "all" }, "errors": [], "results": 4, "paging": { "current": 1, "total": 1 }, "response": [ { "fixture": { "id": 239625, "referee": null, "timezone": "UTC", "date": "2020-02-06T14:00:00+00:00", "timestamp": 1580997600, "periods": { "first": 1580997600, "second": null }, "venue": { "id": 1887, "name": "Stade Municipal", "city": "Oued Zem" }, "status": { "long": "Halftime", "short": "HT", "elapsed": 45 } }, "league": { "id": 200, "name": "Botola Pro", "country": "Morocco", "logo": "https://media.api-sports.io/football/leagues/115.png", "flag": "https://media.api-sports.io/flags/ma.svg", "season": 2019, "round": "Regular Season - 14" }, "teams": { "home": { "id": 967, "name": "Rapide Oued ZEM", "logo": "https://media.api-sports.io/football/teams/967.png", "winner": false }, "away": { "id": 968, "name": "Wydad AC", "logo": "https://media.api-sports.io/football/teams/968.png", "winner": true } }, "goals": { "home": 0, "away": 1 }, "score": { "halftime": { "home": 0, "away": 1 }, "fulltime": { "home": null, "away": null }, "extratime": { "home": null, "away": null }, "penalty": { "home": null, "away": null } } } ] }

commented

Something like this should work:

=IMPORTJSONAPI(<URL>, "$.response[*]", "teams.home.name, teams.away.name, score.halftime.home, score.halftime.away")

Something like this should work:

function IMPORTJSONAPI_MyCustomApi(url, query, cols) {
     var headers = {
         'key_token_myapi': 'myapikey',
         'Accept': 'application/json'
    }
    return IMPORTJSONAPI(url, query, cols, "headers=" + JSON.stringify(headers)) 
}

Which line should I put this? I tried line 1260 but google sheets says undefined function...

Something like this should work:

=IMPORTJSONAPI(<URL>, "$.response[*]", "teams.home.name, teams.away.name, score.halftime.home, score.halftime.away")

I did this but then it returns nothing, not even an error message.... completely blank. What can be the reason? I run your original importjsonapi of course, not my custome function... I run this:

=IMPORTJSONAPI("URL?live=all&api_token='tokenkey'","$.response[*]", "teams.home.name, teams.away.name, score.halftime.home, score.halftime.away")

and even this:

=IMPORTJSONAPI("URL?live=all&api_token='tokenkey'","$..name","@")

but both return a blank sheet, not even an error message...

commented

Try running:

=IMPORTJSONAPI("URL?live=all&api_token='tokenkey'","$","@")

If you still don't see anything then no data is coming back for some reason.

commented

Also try copying the url and pasting it into the browser bar and see what the result is.

Try running:

=IMPORTJSONAPI("URL?live=all&api_token='tokenkey'","$","@")

If you still don't see anything then no data is coming back for some reason.

I get this error message : {"get":"fixtures","parameters":{"live":"all","x-ra...
The browser says missing application key... "errors":{"token":"Error/Missing application key.

My application key is of course correct, but maybe the way we put it in the formula is not correct?

commented

Ok now try this:

=IMPORTJSONAPI("URL?live=all&api_token='tokenkey'","$.response","@")

The browser says missing application key... "errors":{"token":"Error/Missing application key.

My application key is of course correct, but maybe the way we put it in the formula is not correct?

Something like this should work:

function IMPORTJSONAPI_MyCustomApi(url, query, cols) {
     var headers = {
         'key_token_myapi': 'myapikey',
         'Accept': 'application/json'
    }
    return IMPORTJSONAPI(url, query, cols, "headers=" + JSON.stringify(headers)) 
}

Which line should I put this? I tried line 1260 but google sheets says undefined function...Maybe if we put this in the script then our problem is solved

commented

Try adding the function right at the beginning of the file e.g Line 1

Try adding the function right at the beginning of the file e.g Line 1

Thanks sir! It works now, and for the records, it works with this one: ,"$.response[*]", "teams.home.name, teams.away.name, score.halftime.home, score.halftime.away"

Can I use your code to get data form a Sheet(or range of cells) and not from URL? I refer to ImportJSONFromSheet from the original ImportJSON code...

commented

Does not support import from sheet at the moment. But you could add with custom function like this maybe:

function IMPORTJSONFROMSHEET(data, query, cols) {
  var json = JSON.parse(data)
  return IMPORTJSONAPI(json, query, cols)
}

Then use like:

=IMPORTJSONFROMSHEET(C1, $.response[*], "teams.home.name)

where cell C1 contains your json data

Thank you really very much for your help. I need to send a request to the API which brings me back a list based upon one parameter from the API itself and 2 parameters which exist in the same sheet where the formula is (in 2 different cells).... I have problems sending these sheet parameters to the API, as they cannot simply be placed inside the formula, I have to put them outside of the formula (at least it is like this for me). But I can only manage to put one cell reference to the parameter outside of " of the formula, not two. How can I do this in your formula? The formula is:

=IMPORTJSONAPI_apifootball("URL?season=2020&league="&F3;"$.response";"@")

In the above formula, the API parameter itself is season=2020 which is fine. The other parameters which is required by the API are league and team, and their values are inside of the sheet. If only league was needed then I could just put the &F3 (where F3 is the cell value) after the symbol " and it would work. But I do not know how I go around this with 2 parameter values inside the sheet.

Appreciate your help really. Thanks.

commented

Something like this:

=IMPORTJSONAPI_apifootball("URL?season=2020&league="&F3&"&team="&F4;"$.response";"@")

Thanks. I am getting a strange issue here. I have a JSON response where i have another [] inside of a previous []... Now your script cann not return beyond season column (where [standings] comes... What is the issue? How can all or some specific coumn be returned here? I tried both $.response[] and $.response.[*] and even just $ but none worked... The json response sample is:

get": "standings",
"parameters": {
"season": "2020",
"league": "39"
},
"errors": [],
"results": 1,
"paging": {
"current": 1,
"total": 1
},
"response": [
{
"league": {
"id": 39,
"name": "Premier League",
"country": "England",
"season": 2020,
"standings": [
[
{
"rank": 1,
"team": {
"id": 50,
"name": "Manchester City",
},
"points": 71,
"goalsDiff": 43,
"group": "Premier League",
"form": "WWLWW",
"status": "same",
"description": "Promotion - Champions League (Group Stage)",
"all": {
"played": 30,
"win": 22,
"draw": 5,
"lose": 3,
"goals": {
"for": 64,
"against": 21
}
},
"home": {
"played": 16,
"win": 12,
"draw": 2,
"lose": 2,
"goals": {
"for": 36,
"against": 13
}
},
"away": {
"played": 14,
"win": 10,
"draw": 3,
"lose": 1,
"goals": {
"for": 28,
"against": 8
}
},
"update": "2021-03-21T00:00:00+00:00"
},
{
"rank": 2,
"team": {
"id": 33,
"name": "Manchester United",
},
"points": 57,
"goalsDiff": 24,
"group": "Premier League",
"form": "WWDDW",
"status": "same",
"description": "Promotion - Champions League (Group Stage)",
"all": {
"played": 29,
"win": 16,
"draw": 9,
"lose": 4,
"goals": {
"for": 56,
"against": 32
}
},
"home": {
"played": 14,
"win": 7,
"draw": 3,
"lose": 4,
"goals": {
"for": 29,
"against": 19
}
},
"away": {
"played": 15,
"win": 9,
"draw": 6,
"lose": 0,
"goals": {
"for": 27,
"against": 13
}
},
"update": "2021-03-21T00:00:00+00:00"
},
{
"rank": 3,
"team": {
"id": 46,
"name": "Leicester",
},
"points": 56,
"goalsDiff": 21,
"group": "Premier League",
"form": "WWDLW",
"status": "same",
"description": "Promotion - Champions League (Group Stage)",
"all": {
"played": 29,
"win": 17,
"draw": 5,
"lose": 7,
"goals": {
"for": 53,
"against": 32
}
},
"home": {
"played": 14,
"win": 7,
"draw": 1,
"lose": 6,
"goals": {
"for": 25,
"against": 19
}
},
"away": {
"played": 15,
"win": 10,
"draw": 4,
"lose": 1,
"goals": {
"for": 28,
"against": 13
}
},
"update": "2021-03-21T00:00:00+00:00"
},
{
"rank": 4,
"team": {
"id": 49,
"name": "Chelsea",
},
"points": 51,
"goalsDiff": 19,
"group": "Premier League",
"form": "DWWDD",
"status": "same",
"description": "Promotion - Champions League (Group Stage)",
"all": {
"played": 29,
"win": 14,
"draw": 9,
"lose": 6,
"goals": {
"for": 44,
"against": 25
}
},
"home": {
"played": 14,
"win": 7,
"draw": 5,
"lose": 2,
"goals": {
"for": 25,
"against": 11
}
},
"away": {
"played": 15,
"win": 7,
"draw": 4,
"lose": 4,
"goals": {
"for": 19,
"against": 14
}
},
"update": "2021-03-21T00:00:00+00:00"
},
{
"rank": 5,
"team": {
"id": 48,
"name": "West Ham",
},
"points": 49,
"goalsDiff": 10,
"group": "Premier League",
"form": "DLWLW",
"status": "same",
"description": "Promotion - Europa League (Group Stage)",
"all": {
"played": 29,
"win": 14,
"draw": 7,
"lose": 8,
"goals": {
"for": 45,
"against": 35
}
},
"home": {
"played": 15,
"win": 8,
"draw": 4,
"lose": 3,
"goals": {
"for": 26,
"against": 18
}
},
"away": {
"played": 14,
"win": 6,
"draw": 3,
"lose": 5,
"goals": {
"for": 19,
"against": 17
}
},
"update": "2021-03-21T00:00:00+00:00"
},
{
"rank": 6,
"team": {
"id": 47,
"name": "Tottenham",
},
"points": 48,
"goalsDiff": 19,
"group": "Premier League",
"form": "WLWWW",
"status": "same",
"all": {
"played": 29,
"win": 14,
"draw": 6,
"lose": 9,
"goals": {
"for": 49,
"against": 30
}
},
"home": {
"played": 14,
"win": 7,
"draw": 3,
"lose": 4,
"goals": {
"for": 25,
"against": 14
}
},
"away": {
"played": 15,
"win": 7,
"draw": 3,
"lose": 5,
"goals": {
"for": 24,
"against": 16
}
},
"update": "2021-03-21T00:00:00+00:00"
},
{
"rank": 7,
"team": {
"id": 40,
"name": "Liverpool",
},
"points": 46,
"goalsDiff": 12,
"group": "Premier League",
"form": "WLLWL",
"status": "same",
"all": {
"played": 29,
"win": 13,
"draw": 7,
"lose": 9,
"goals": {
"for": 48,
"against": 36
}
},
"home": {
"played": 15,
"win": 7,
"draw": 2,
"lose": 6,
"goals": {
"for": 22,
"against": 18
}
},
"away": {
"played": 14,
"win": 6,
"draw": 5,
"lose": 3,
"goals": {
"for": 26,
"against": 18
}
},
"update": "2021-03-21T00:00:00+00:00"
},
{
"rank": 8,
"team": {
"id": 45,
"name": "Everton",
},
"points": 46,
"goalsDiff": 3,
"group": "Premier League",
"form": "LLWWW",
"status": "same",
"all": {
"played": 28,
"win": 14,
"draw": 4,
"lose": 10,
"goals": {
"for": 40,
"against": 37
}
},
"home": {
"played": 14,
"win": 5,
"draw": 2,
"lose": 7,
"goals": {
"for": 19,
"against": 22
}

Any idea above last question above? Appreciate it :)