If you have made an API request inside of your ParseHub project and are looking to scrape/interact with the .json response you have more options than just treating it as raw text for extraction.
The steps outlined below will show you how to convert text to JSON using ParseHub's $jsonify and $JSON.parse() methods.
Step 1: Make an API call
For this guide we are going to use the REST Countries API, which allows users to retrieve data sets containing useful information for various countries and regions. Let's use an API URL that will return information for both Australia and Colombia in the form of two JSON objects inside a list: https://restcountries.eu/rest/v2/alpha?codes=au;col
To start, let's make a new ParseHub project. The starting site can be anything. Once your project is ready to go let's add a 'Go To Template' command that we can use to make our API call.
Adding the command will open a configuration window. In this window we are going to paste in our API URL wrapped in "double quotes" as the URL setting. We are also going to tell ParseHub to go to a new template called 'json'.
If ParseHub does not open the URL automatically after we finalize the command you can just use Browse Mode to open a new tab and paste the URL in manually.
Step 2: Scraping the response
You should now be able to see the API response on screen. It should look like this:
Target the response with a select command. You will see that ParseHub extracts the text with $e.text by default. This is all well and good, but not very useful when it comes to using this data post extraction.
To convert this data from text to an actual JSON object we can use this extraction setting: $JSON.parse($e.text) You will notice that this extracts something into our data called Object. This is because our data is now in an Object format that ParseHub cannot reasonably display for you.
We will need to unpack this JSON Object so that ParseHub can spread out the data inside it as a part of your own dataset. This can be done by placing our existing extraction inside the $jsonify() method, like this: $jsonify($JSON.parse($e.text)) Once you do this you can see ParseHub extracting the data into the data preview window as actual json, which is great!
Step 3: Isolating specific data
It's quite unlikely that you want the entire JSON response from the API call. We can isolate specific data by using some object notation. Firstly, if we take a look at the API response, we can see that it is essentially a list containing two JSON objects in this format: [{australia_data}, {colombia_data}]
If you only wanted to scrape the data for Colombia we could modify our fullObject extraction command to use this setting: $jsonify($JSON.parse($e.text)[1])
Let's focus on isolating individual pieces of the data with some additional extraction commands that reference our existing fullObject extraction. If you wanted to extract the 'population' value for Australia you could do so by making a new extraction command and using the extraction setting: fullObject[0].population
fullObject[0] refers to the first object in our data, Australia, and .population refers to the value assigned to the key "population" in that dataset. Here are some further examples:
Australian Capital: fullObject[0].capital ----> "Canberra"
Australian Region: fullObject[0].region ----> "Oceania"
Colombia Capital: fullObject[1].capital ----> "Bogotá"
Colombia Region: fullObject[1].region ----> "Americas"
Step 4: (Optional) removing the full data
If you would like to remove the full dataset extraction from your data after grabbing everything you need you can do so by adding another extract command also called fullObject and setting it to extract the value 0. This is essentially overwriting the existing extraction value and acting as a delete option.
Congratulation you made it to the end! Contact us if you have questions!