Skip to main content
Skip table of contents

Construct JSON

Building a JSON object is a way to organize data in a simple, easily accessible way for a web application to read and use. Let's say that we want to export data containing the name, age and city of residence of a user. We decide to organize it in the simplest way, according to the JSON syntax:

{name: "John", age: 31, city: "New York"}

In order to build and export this JSON, we can use the Concatenate standard Grasshopper component which concatenates pieces of text together. The example above can be obtained with a simple definition:

However, when objects get bigger and more complex, this approach stopS being viable, which is why we created the JSON construct component.

The Construct JSON component simplifies the process of putting together a string of characters representing an object. It also converts Grasshopper data types into JSON format doing basically the opposite of the ShapeDiverJSONConvert component.

This component has 3 inputs, an optional input for a previous JSON object that you have already built and you want to add data to, an input with the data you want to put inside the JSON, and the “AddressString” that is the name you will give to that piece of data so that you can easily find it later.

In the example above, we would first add the name property, then the age, and finally the city:

Alternatively, if you are comfortable with Grasshopper tree structures, building this object can be more succinct. It is possible to assign several properties at once by giving a list of addresses corresponding one-to-one with a tree of values. The same result is obtained with the following definition:

You can create all kind of JSON data structures, from a simple object like the one above, to arrays, arrays of objects, objects inside objects, etc. Additionally, these JSON objects can be accessed via the API by sending them through a Direct Data Output component.

The data that can be used to fill a JSON object can be of one of several types: numbers, points, strings, lists which will be converted to arrays, etc. If you input more complex data such as boxes, lines, intervals, planes, nurbs surfaces, nurbs curves, colors and transformations, the component will output a Javascript object with a “type” attribute and a “data” attribute with the necessary information to reproduce the given data. For example, if you input a line, the JSON convert will give as “type”:“line” and the “data” is an array of two arrays which contains the X, Y and Z values of the start and end points.

Use the Data Output Component
In order to be able to access the constructed JSON via a web application, input the JSON to the Direct Data Output component and give the component a name. This name will be what you need to search using the ShapeDiver API in order to get the information.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.