In today’s post, we will discuss how to create a Dynamo node that makes GET requests to a web API, either on the global internet or a local network, so that it returns a JSON string and then how to use the built-in Dynamo Python nodes to parse the JSON string into something readable by Dynamo. Those who watched our previous Dynamo Dictionaries video will recognize the output JSON as a Dictionary object that can be worked with easily later.
Our first step is to create a new Visual Studio project using the zeroTouch Dynamo Template.
This template can be found by downloading the Dynamo Dev Starter Kit using Visual Studio’s extensions library. See the image below for reference. More information can also be found on this Github Page. I also want to thank Github user alvpickmans for making this. Follow along in the video to see how to change the Dynamo version to 2.6 for Revit 2021.
We will then need to create the static class that Dynamo will turn into a node. We need to make the class public so that Dynamo can find it. We make it static so that Dynamo can run it functionally. Dynamo recognizes class objects but even those need to have static functions that work as pseudo-constructors. Anyhow, for now, the code is below:
public static class WebUtilities
{
/// <summary>
/// Runs a get request to a particular URL
/// </summary>
/// <param name="url">URL of the </param>
/// <param name="refresh">Change value to tell Dynamo to update output</param>
/// <returns>A greeting</returns>
public static string GetFromUrl(string url, bool refresh)
{
// ping the url and get the response
WebRequest getUrl = WebRequest.Create(url);
// get the response stream
Stream objStream = getUrl.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader(objStream);
// get the response to a string
string sLine = "";
string returnString = "";
int i = 0;
while (sLine != null)
{
i++;
sLine = objReader.ReadLine();
returnString += sLine;
}
return returnString;
}
}
The logic for the code is as follows: We take in a URL string and use that to make a WebRequest. We then turn the response to that WebRequest into a Stream of bytes that we can read as a string. We run a while loop that tracks lines of strings that come out of the StreamReader. The StreamReader will return null when it runs out of response string so we know that we are done with the response. We then return the string that we have built.
Before we get started, we’ll need to find a URL that returns a JSON string. Since this a just a demo, I’ll use this URL to get the point across: https://random-data-api.com/api/address/random_address. An example for an actual application would be something like an API end point that returns Site Information or Building Information that can then be accessed in Dynamo.
Here’s the output from our node:
You can see from that watch node that it comes out as just a string. The string is not particularly useful to us as far as accessing and using its information.
We can change that by using a pretty simple Python Script to parse the JSON string into a Dictionary for Dynamo to use.
# Load the Python Standard and DesignScript Libraries
import sys
sys.path.append("C:\Program Files (x86)\IronPython 2.7\Lib")
import json
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN
# Place your code below this line
unparsed = IN[0]
parsed = json.loads(unparsed)
# Assign your output to the OUT variable.
OUT = parsed
This script really only relies on one line of code: json.loads(unparsed). This can be found in Python’s documentation here. But essentially it turns a string that’s in JSON format and turns it into a Python Dictionary that Dynamo can access. In order to use this though, you’ll notice that there is the sys.path.append at the top where we import libraries. This tells Dynamo where to find our IronPython JSON library. We won’t be able to use the JSON library otherwise so this is a very important step.
And there you have it, we have created a Dynamo node that can turn a JSON api endpoint on the web into a dictionary that Dynamo can read. Follow along on the video for more step-by-step instructions. Also, to learn more about Dictionaries, check out our past posts on using Dynamo’s Dictionaries (part 1 and part 2).
About the Author
Chris Woodward
Design Technologist As a design technologist, Chris combines a true fascination with both design and technology to streamline design processes and construct computational design solutions. His interest in coding can be traced back to video games mods. A more rigorous exploration started as an M.Arch Student at the University of Illinois Champaign-Urbana where he dug deep into Rhino and Grasshopper. Then, while working as an architectural designer, Chris brought this technology interest into world BIM with Revit and Dynamo. Along the way, Chris has learned a myriad of programming languages such as Python, JavaScript and C# allowing him further access to custom solutions. Not one to get too caught up in just one thing, Chris supplements has supplemented his passion in design technology with a number of hobbies and interests including mechanical keyboards, urban design, web design, and a budding interest in fountain pens. Chris can also often also be found outdoors on long-distance run or hitting the tennis or basketball courts.