HomeiOS Developmentios - How do I solely encode as much as a sure...

ios – How do I solely encode as much as a sure depth in Swift utilizing JSON


I am working with House Assistant’s REST Api and is utilizing the /api/state route.
That is an instance of a response:

{
  "entity_id": "replace.home_assistant_supervisor_update",
  "state": "off",
  "attributes": {
    "auto_update": true,
    "installed_version": "2023.09.2",
    "in_progress": false,
    "latest_version": "2023.09.2",
    "release_summary": null,
    "release_url": "https://github.com/home-assistant/supervisor/releases/tag/2023.09.2",
    "skipped_version": null,
    "title": "House Assistant Supervisor",
    "entity_picture": "https://manufacturers.home-assistant.io/hassio/icon.png",
    "friendly_name": "House Assistant Supervisor Replace",
    "supported_features": 1
  },
  "last_changed": "2023-09-13T19:47:40.531114+00:00",
  "last_updated": "2023-09-13T19:47:40.531114+00:00",
  "context": {
    "id": "01HA81BHBKRX165DKPK0Q1VZ8C",
    "parent_id": null,
    "user_id": null
  }
}

The issue is that the attributes object is totally different for each kind of machine.
So what I wish to do is make it so the entity_id, state, last_changed and last_updated are in there common decoded kind. Nonetheless for attributes and content material, they’re unencoded strings of JSON.

An instance of what this could appear to be:

{
  "entity_id": "replace.home_assistant_supervisor_update",
  "state": "off",
  "attributes": '{"auto_update":true,"installed_version":"2023.09.2","in_progress":false,"latest_version":"2023.09.2","release_summary":null,"release_url":"https://github.com/home-assistant/supervisor/releases/tag/2023.09.2","skipped_version":null,"title":"House Assistant Supervisor","entity_picture":"https://manufacturers.home-assistant.io/hassio/icon.png","friendly_name":"House Assistant Supervisor Replace","supported_features":1}',
  "last_changed": "2023-09-13T19:47:40.531114+00:00",
  "last_updated": "2023-09-13T19:47:40.531114+00:00",
  "context": '{"id": "01HA81BHBKRX165DKPK0Q1VZ8C","parent_id": null,"user_id": null}"'
}

It is actually complicated however I have been in search of some time and I am unable to discover an precise answer.
Thanks!

I attempted wanting anyplace, I had nowhere to start out.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments