Objects Blocks
Last updated
Last updated
The objects blocks allow you to organize and manage information as structured entities, like pieces of data bundled together. You can use these blocks to create and handle collections of related information.
The objects blocks are often used in conjunction with Web API Blocks, the Map Component and Location Sensors, which often send data in object format.
The "set property of object to" block allows you to change the value of a single property of an object.
The "get property of object" block allows you to read the value of an object's property. If the object does not have a property with this name, it will return "undefined."
The "get property of object" block can be used to get nested values, i.e. properties of properties of an object. If you are trying to get nested property b of property a of an object, you can simply get property a.b of an object, instead of using multiple "get property of object" blocks.
Below are two example of getting a property of a nested object. The first is presented with the object written in JSON format and the second is created with objects blocks, but both sets of blocks do exactly the same thing. Each will return 5.
You can also get a value from an array (or list) of values. If an object property b is a 3-items list, and you want to get the second item of that list, you can get property b[2] of that object. The first item of the list is at index 1.
Below are two example of getting a property from an array. The first is presented with the object written in JSON format and the second is created with objects blocks, but both sets of blocks do exactly the same thing. Each will return 6.
The "get property of object" block can also be used to get a property from an array that is a nested property of an object.
Below are two example of getting a property from an array that is a nested property of an object. The first is presented with the object written in JSON format and the second is created with objects blocks, but both sets of blocks do exactly the same thing. Each will return 7.
If an API returns a JSON (JavaScript Object Notation) value, you can convert it to an object using the "get object from JSON" block.
To convert an object to JSON to make an API POST or set headers or query parameters for your API call use the "generate JSON from object" block.
The "create object" block allows you to create a new object. Once created, objects can be uploaded to your Firebase Realtime Database as the value of a cloud variable.
To add additional properties to the object:
Drag the "create object" block into your workspace.
Click the gear icon.
In the open modal, drag as many empty property fields as you need from the left and connect them to the block on the right.
Click inside the newly connected block to type the name of the property field.
Click the gear icon when you're done adding property fields.
The "get object properties of" block returns a list of property names for an object. It is often used with list blocks.
Objects blocks are often used when making calls to an API. It's common for an API to provide you with a large amount of data in JSON format, similar to the following:
In the block combination below, when the screen starts, it calls the web API and receives the response, status, and error outputs. If an error occurs, the label's text is set to the error output message. If no error occurs, the JSON response output is converted to an object. From that object, the nested "temp_f" property of the "current_observation" property is joined with "°F" to populate the label's text.