Get part of an API Response(JSON)

Post Reply
bkromer
Member
Posts: 99
Joined: Thu Jul 11, 2019 10:41 am

Get part of an API Response(JSON)

Post by bkromer »

Hello,

I am making an HTTP call to the ninox API:

Code: Select all

// VIA API TO NINOX DATABASE
		var url = 'https://api.ninoxdb.de/v1/teams/uvasdadarz7twsCfQ/databases/ooysgasdassdlnp/tables/S/records';
		var key = 'lalalalalalalalalalalalallalalallaa;
		var json = new ByteArray(
		'{"fields":{"Datei":"' +
			materialnummernRE.cap(j) +
			'","Version der Datei":"' +
			materialnummernRE.cap(j) +
				'","Faltanweisung":"' +
			materialnummernRE.cap(j) +
				'","Version der Faltanweisung":"' +
			materialnummernRE.cap(j) +
				'","Position":"' +
			i +
				'","Materialnummer":"' +
			materialnummernRE.cap(j) +
				'","Materialbezeichnung":"' +
			"-" +
				'","Auftragsmenge":"' +
			auftragsmengen[i].toString +
				'","Nettopreis":"' +
			nettopreise[i].toString +
				'","Liefertermin":"' +
			lieferterminRE.cap(j) +
			'","Preiseinheit":"' +
			preiseinheitRE.cap(j) +
			'","Bestellnummer":"' +
			bestellnummer +
			'","Dateiname":"' +
			filename +
			'","Bestelldatum":"' +
			bestelldatum +
			'"}}',
		'UTF-8'
		);
		var theHTTP = new HTTP(HTTP.SSL);
		theHTTP.authScheme = HTTP.OauthAuth;
		theHTTP.authorization = 'Bearer ' + key;
		theHTTP.url = url;
		theHTTP.setPostData(json, 'application/json');
		theHTTP.post();
		while (!theHTTP.waitForFinished(3)) {
			job.log(1, 'API CALL IN PROGRESS!');
		}
	
		if (theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200) {
			job.log(1, 'API CALL successfull RESPONSE: ' + theHTTP.getServerResponse().toString('UTF-8'));
			
		} else {
			job.log(
				3,
				'API CALL Failed:  ' +
					theHTTP.getServerResponse().toString('UTF-8') +
					'  Status Code: ' +
					theHTTP.statusCode.toString('UTF-8')
			);
		}
			 // ENDE API CALL
I am getting back a JSON response:

Code: Select all

{
  "id": 654,
  "createdAt": "2020-03-06T08:45:25",
  "createdBy": "admin",
  "modifiedAt": "2020-03-06T08:45:25",
  "modifiedBy": "admin",
  "fields": {
    "Position": "0",
    "Dateiname": "C_BestellungMitZweiPositionenUndDruckdatei.txt",
    "Faltanweisung": "160777 ",
    "Materialnummer": "160777 ",
    "Materialbezeichnung": "-",
    "Auftragsmenge": "function",
    "Nettopreis": "function",
    "Bestelldatum": "02.03.2020 ",
    "Liefertermin": "01.04.2020",
    "Preiseinheit": "EUR / 1000 ST ",
    "Bestellnummer": "4500047702",
    "Datei": "160777 ",
    "Version der Datei": "160777 ",
    "Version der Faltanweisung": "160777 "
  }
}
For my next API call I need the id of this JSON.
I think I have to parse "theHTTP.getServerResponse();" before i can enter id with dot notation? Is this right? How can I make this work?

Thanks in Advance
Ben
Benjamin
mkayyyy
Member
Posts: 79
Joined: Mon Nov 21, 2016 6:31 pm
Location: UK

Re: Get part of an API Response(JSON)

Post by mkayyyy »

You will need to use JSON.parse() function so that you can get to the id:

Code: Select all

var parsedResponse = JSON.parse(theHTTP.getServerResponse().toString('UTF-8'));
parsedResponse.id;
sander
Advanced member
Posts: 274
Joined: Wed Oct 01, 2014 8:58 am
Location: The Netherlands

Re: Get part of an API Response(JSON)

Post by sander »

Code: Select all

try {
	jsonReturn = JSON.parse(theHttp.getServerResponse().toString('UTF-8'));
	log(2, jsonReturn.id);
} catch(theError) {
	log(2,'json parse error: ' +theError);
}
Something like this I think :)
bkromer
Member
Posts: 99
Joined: Thu Jul 11, 2019 10:41 am

Re: Get part of an API Response(JSON)

Post by bkromer »

Thank you sander!
Works perfectly. :)
Benjamin
bkromer
Member
Posts: 99
Joined: Thu Jul 11, 2019 10:41 am

Re: Get part of an API Response(JSON)

Post by bkromer »

Is there a way to make a variable global? So that I can use the id in global scope?
Benjamin
sander
Advanced member
Posts: 274
Joined: Wed Oct 01, 2014 8:58 am
Location: The Netherlands

Re: Get part of an API Response(JSON)

Post by sander »

Define your var outside your function or use return?
bkromer
Member
Posts: 99
Joined: Thu Jul 11, 2019 10:41 am

Re: Get part of an API Response(JSON)

Post by bkromer »

Thanks again. Works! :D
Benjamin
Post Reply