Skip to content

JSON Manipulation

  • PARSE_JSON <topic_message> TO <variable_name>: Decode a JSON message from a topic.
  • COMBINE <variable_1> AND <variable_2> INTO <json_variable>: Construct a JSON structure from two variables.
  • EXTRACT <json_key> FROM <json_variable> TO <new_variable>: Extract a particular value from a JSON structure.

Example - JSON Manipulation

This script listens for messages on the /temperature and /humidity MQTT topics, and then combines these values into a JSON object which is published to the /climateData MQTT topic.

ON MQTT_TOPIC /temperature
    SET tempValue FROM /temperature MESSAGE

ON MQTT_TOPIC /humidity
    SET humidityValue FROM /humidity MESSAGE

ON BOTH /temperature AND /humidity RECEIVED
    COMBINE tempValue AND humidityValue INTO climateData  
    PUBLISH /climateData WITH climateData 
Explanation
  1. Set Temperature Value When a message is received on the /temperature MQTT topic, the tempValue variable is set to the message received.

    ON MQTT_TOPIC /temperature
        SET tempValue FROM /temperature MESSAGE
    
  2. Set Humidity Value Similarly, when a message is received on the /humidity MQTT topic, the humidityValue variable is set to the message received.

    ON MQTT_TOPIC /humidity
        SET humidityValue FROM /humidity MESSAGE
    
  3. Combine and Publish When messages have been received on both the /temperature and /humidity topics, the tempValue and humidityValue variables are combined into a JSON object climateData, and then published to the /climateData MQTT topic.

    ON BOTH /temperature AND /humidity RECEIVED
        COMBINE tempValue AND humidityValue INTO climateData
        PUBLISH /climateData WITH climateData
    

    For example, if the tempValue received is 25 and the humidityValue received is 60, the climateData object will be {"temperature": 25, "humidity": 60} and this will be published to the /climateData MQTT topic.

FAQ - JSON Manipulation

What types of JSON manipulations can I perform with Flux?

Flux allows various JSON manipulations such as parsing JSON strings, extracting values, combining multiple values into a JSON object, and more.

Can I combine values from different sources into a single JSON object?

Yes, with the COMBINE command, you can combine multiple values into a single JSON object.

How can I extract a specific value from a JSON object?

You can use the EXTRACT command to extract a specific value from a JSON object.

Can I replace a value in a JSON object?

Yes, you can use the REPLACE command to replace a value in a JSON object.

Is it possible to convert a JSON object to a string?

Currently, Flux does not provide a direct command to convert a JSON object to a string. However, you can use the CONCAT command to concatenate JSON object values into a string.

Can I manipulate JSON arrays with Flux?

Flux primarily focuses on JSON objects, and does not provide specific commands for manipulating JSON arrays. However, you can manipulate JSON arrays as strings using the CONCAT, REPLACE, and other string manipulation commands.