loadjson()

Creates a JSON object based on the text retrieved from the supplied location. The function lets you retrieve content from a JSON enabled server using a standard HTTP request. Popular content management systems like WordPress (requires JSON API plugin) and Drupal provide a JSON service/API to retrieve content.

Note:

  • The specified JSON file is expected to be UTF-8, UTF-16 or UTF-32 encoded. A byte order mark specified as the first character will be stripped. Saving any changes will change the encoding to UTF-8.
  • Loadjson() is cached per batch run (based on the URL) in print/email.
  • This online JSON viewer is handy to debug JSON data: http://jsonviewer.stack.hu​

Tip: External content is not loaded while editing a script. To test a script that loads external content, you can do a preflight; see Doing a Preflight.

Note:
In OL Connect 2025.1 stricter checking was introduced for the Uniform Resource Identifier (URI) passed into the loadhtml, loadjson and loadtext scripting functions.
Special characters such as spaces in a path used to work but now need to be properly URI encoded.

Example:
// Worked in 2024.2 but not in 2025.1
var result = loadhtml("file:///C:/PPWorkDir/Hello World/Data/Example.html");
// 2025.1 onwards
var result = loadhtml("file:///C:/PPWorkDir/Hello%20World/Data/Example.html");


This applies to more than just the space character. Refer to RFC 2396 for the rules on URI encoding.

loadjson(location)

Loads json data from the specified location.

location

String; the supplied location should be either a URL or a relative file path. The file protocol is supported as well as the http/https protocols.

The complete syntax of a fully qualified URL with the "file" protocol is: file://<host>/<path>. If the host is "localhost", it can be omitted, resulting in file:///<path>, for example: file:///c:/somefolder/somejson.json.

Examples

This sample script retrieves JSON data from a snippet.

Copy
var localJSON = loadjson('snippets/jsonsnippet.json');
if(localJSON.post){     
    results.html("<h3>" + localJSON.post.title + "</h3><p>" + localJSON.post.modified + "</p>");
}

This script retrieves a post from a WordPress site.

Copy
var wpPost = loadjson('http://192.168.101.58/2013/06/leave-the-third-dimension-behind-and-focus-on-real-printing-innovation/?json=1'); ​
if(wpPost.post){ ​​    
    results.html("<h1>" + wpPost.post.title + "</h1>" 
         + wpPost.post.content);
​}

This script retrieves multiple posts from a WordPress site.

Copy
var numPosts = 3;
var wpPost = '';
var wpRecentPosts = loadjson('http://192.168.101.58/?json=get_recent_posts&count=' + numPosts);
if(wpRecentPosts.posts){    
    for (var i = 0; i < numPosts ; i++) {        
        wpPost += "<p>" + wpRecentPosts.posts[i].title + "</p>";     
    } 

results.after(wpPost)