Change msg.topic dynamically

You just create an object, and set it as a global variable. It should be the same as uploading a string or a number.

Here's an example of how you see the uploaded objects (I put a mosaic on one of the names for privacy issues). I nested several objects, each one of them represents a robot, with an update frequency that depends on the task they're doing in that moment.

Idle robots will update every 5 seconds, charging robots every 10 or so, navigating robots update more often, and docking robots even faster, to check for the sensors. If I had to join messages, it would mean updating all of them at the same frequency, or having a complicated join setup. As it is now, each robot updates and writes its sensors' data and status in its part of the object.

At any given moment, I can read the whole object and put it on a message, so I get the current status on each robot at the same time.

1 Like

that sounds very intelligent :+1:

can you show me the function behind / small part of it / example ?
I have no idea how to create such a large nested object, but I would be interested!

In this JSON objects tutorial there's a part about nested objects. It's really easy to follow, with examples.

You just have to create the object in one of your function nodes, then store the whole thing in global context as it's explained in the documentation about working with context.

1 Like

Wow, I can't believe w3schools has a page labelled "JSON objects"

For anyone else reading...
"JSON object" is a misnomer. JSON stands for "JavaScript Object Notation". Essentially, JSON is a string representation of a JavaScript object

You beat me to it. I was in the process of typing a similar message. The error on the page is ironic because in the intro to that section it clearly describes the difference, and then goes on to conflate the two in the page linked here.

1 Like

Point taken. However, and regardless of the naming inaccuracy in the title, their tutorials are understandable and easy to follow.

I think that, like most of their content, is oriented at people who enter their site without having a clue, and therefore will forgive a mistake like that. I've followed their tutorials for years, and they're one of the first places I search for any HTML/CSS/JS-related issues.

1 Like

I fully agree, that is why it is so surprising to see such an error.

People who haven't a clue won't know it is a mistake, that is why it is important to get things right.

There is a Report Error button at the bottom of the page, which pops up the message
"If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail: help@w3schools.com"
I suggest anyone interested here does that.

Waaaay ahead of you Colin (I did that 1hr ago :joy::joy::joy:)

Some of us have a life to lead away from here :slight_smile:

Cheeky :wink:

Anyhow, I bet it took less time to email them than write your post haha

1 Like

Seems like they fixed it already. They changed the name of the section to "JSON Object Literals", and added a note:

It is a common mistake to call JSON object literals "JSON objects" because their syntax is (almost) identical to JavaScript objects.
JSON is based on JavaScript object literals. A JavaScript object literal does not need quotes around the property names, and JavaScript can use single quotes, but JSON cannot.
JSON is a "data-interchange-format". The data is actually only JSON when it is in a string format. When it is converted to a JavaScript variable, it becomes a JavaScript object.

1 Like

Excellent. Though I think this is still not strictly correct:

You can create a JavaScript object from a JSON object literal:

Example

myObj = { "name":"John", "age":30, "car":null };

I don't think that is a JSON object literal (in that context), it is just javascript.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.