Device Twin Array

SnackMahana 21 Reputation points
2022-01-11T18:09:31.567+00:00

Hello,

Can I include an array into my desired properties? Are there examples? I'm having difficulty implementing arrays into my twin desired properties.

Thanks

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,272 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Matthijs van der Veer 4,376 Reputation points MVP Volunteer Moderator
    2022-01-12T07:36:41.397+00:00

    Like @AshokPeddakotla-MSFT mentioned, you can definitely put arrays in your twin. But the link to that blog post isn't using real arrays, but a workaround from a time where arrays weren't supported yet. Here's an example of how to add an array to your twin using the REST API.

    You can send a PATCH message to https://fully-qualified-iothubname.azure-devices.net/twins/{id}?api-version=2020-05-31-preview with the following body:

    {  
        "properties": {  
            "desired": {  
                "iDidAnArray": [  
                    "foo", "bar"  
                ],  
                "arrayWithObjects": [  
                    { "prop1": "foo" },  
                    { "prop2": "bar" }  
                ]  
            }  
        }  
    }  
    

    I included an array of strings, and an array with objects.

    You can then verify in the portal (or by doing a GET request to the same endpoint) that the arrays were added:
    arrays in the device twin

    2 people found this answer helpful.
    0 comments No comments

  2. AshokPeddakotla-MSFT 35,971 Reputation points Moderator
    2022-01-12T07:12:55.803+00:00

    @SnackMahana Please refer to the below information.

    Can I include an array into my desired properties?

    Yes, IoTHub has added support for JSON arrays inside device twins. Please see here for more details.

    164070-image.png

    Are there examples?

    Please refer to Sample desired properties and also Adding an array to your twin desired properties(module)

    I'm having difficulty implementing arrays into my twin desired properties.

    What have you tried so far and what is the issue you are facing? Can you please provide more details?

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.