Importing JSON Data From a Web Service in Origin 2019


Improvements in Origin 2019 make it easier to request data from a web service and import it into an Origin workbook. The two improvements involve:

  • Improved support for making HTTP requests in Origin C.
  • More versatile support for managing JSON data in Origin C.

In previous versions of Origin, it took a combination of the web2file X-Function to make an HTTP request and save the result to a file, then load the file into a string, then convert JSON data to a struct, and finally extract data from the struct, to achieve the same results that are now easy to do.

In this blog post, we will see a example of making an HTTP GET request for some JSON data and importing it into an Origin worksheet. While Excel is able to do this, we feel that it is much easier to do the same thing in Origin C.

HTTP Requests

The okutil_http_get function is available in Origin 2019 (see below). If it returns  0  , then the request was made successfully and the results are returned in a pointer to a string.


Prior to Origin 2019, JSON data could be managed in Origin C using structs. While that is a good mechanism, dealing with structs can be difficult and a bit inflexible. So, starting with Origin 2019,  JSON can be converted into an Origin C TreeNode. Origin C has a rich API for dealing with TreeNodes, so it is a very good way to manage JSON data.

To convert a string of JSON data to a TreeNode, simple use the JSON.FromString  method as follows:

Additionally, there is a function that is especially useful for dealing with extracting data from the TreeNode:

This extracts a vector from a given TreeNode when that TreeNode contains data that was originally a scalar array in the JSON data.

Let’s look at a simple example of how easy it is to use these functions to import JSON data from a web service into an Origin worksheet.


In the following example, we will query the Open Spectral Database for the IR Spectrum of Ceric Oxide. The request will be made using  okutil_http_get  passing it the URI  We will then convert the JSON response string into a TreeNode. Then, using the proper TreeNode “path syntax”, we will  extract the X and Y data vectors and put them into the active worksheet. For reference,  the structure of the TreeNode associated with the JSON data is presented at the end of this post.

Note: you can copy the code below and paste it into an Origin C file and compile it to run the example.

More Advanced Functionality

We can expand upon the above example to illustrate two things: more thorough error checking for the HTTP GET  request and extracting individual strings to use for column long names. The changes to the above code are denoted in comments and highlighted.

Additionally, there is a TreeNode method, GetNodeFromPath , that returns a TreeNode based on a string path. It is defined as:

It can be useful when you do not have a pre-defined TreeNode for the JSON data. In the following example, we have created a reusuable function to load data into a column based on a path string. This technique allows for code reuse and allows for clearer, simpler code. Observe how much shorter our main function is.


We have illustrated how easy it can be to get data from a web service as JSON and import it into Origin. Of course, these are simple examples but they form the backbone of more complex import routines. Finally, please note that support for HTTP request headers will  become available in Origin 2019b.

JSON Data TreeNode Structure

In the code below, the nodes that we are using are highlighted.


Related Posts

About Chris Drozdowski

Chris Drozdowski is a Product Support Engineer at OriginLab. He loves to talk to customers and educate them. He particularly relishes diagnosing and solving difficult, edge-case issues. As well, he contributes code to help solve problems or enhance user experience. In his down time at work, he likes to research and write about esoteric product features. Outside of work, he enjoys spending time with his family, learning even more about coding, and exploring craft beers.

View all posts by Chris Drozdowski →

Leave a Reply

Your email address will not be published. Required fields are marked *