Parsing the Results
If we wanted to extract a single value ouf of the results, we have a number of approaches. One useful approach would be to turn this JSON string into a Python Dictionary. This way we can single out a key instead of using regex or looking for substrings (both valid approaches in their own regard).
When presented with a JSON string, we can call system.util.jsonDecode() to turn a JSON string into a native Python Object. Thus, we can modify our code to the following:
Now we can easily retrieve a single value bay specifying key names on the results.
Make the Results Human Readable
Now that we know how to extract the results, we should clean up the output of the GET call. The JSON string returned by the endpoint could potentially be long and cumbersome to read through for a human, but we can use Python's built-in pprint library to pretty print the results.
The resulting output, which is much easier to read, looks like the following:
From here we can see all of the keys that lead to our final value:
To get to the value for the sunset key, we simply need to address each key along the way:
Troubleshooting HTTP Methods
When making HTTP calls, it is helpful to be familiar with the status codes that are returned by errors. To demonstrate, we could modify an earlier example:
This will return an error, which looks like the following:
Note that HTTP response code 400, which means bad request, was referenced. This error code is correct because we intentionally used an incomplete address!
HTTP Response Codes
The World Wide Web Consortium has a page dedicated to HTTP response codes, which details all possible error codes. However, several common codes are listed below:
|400||Bad Request - The server could not understand the request due to malformed syntax.|
|401||Unauthorized - The request requires some sort of authentication. Potentially some user credentials or an auth token of some kind.|
|403||Forbidden - The server understood what you requested, but is intentionally refusing the request. In some cases, the error message may include a reason why the request was not fulfilled (but not always). Typically, if the server doesn't include a reason, they'll use a 404 error code instead|
|404||Not Found - Your syntax was correct, but the server could not find the resource you were asking for. This could mean a typo, or missing portion of the URL you are using. In this case, double check the address you're specifying. Depending on the configuration, this could also mean that the server does actually have the resource you requested, but doesn't want to confirm its existence (typically due to a security policy. See error code 403 above).|