Skip to main content

Hello

I'm early in my JAMF API & Swift learning curve. I have worked through the wonderful Swift/JAMF API tutorial at:
Part 2: https://www.jamf.com/blog/mac-admin-swift-jamf-pro-api-decoding-data-objects/

and I am now attempting to expand the reported data pulled from the JSON. I have no problem pulling additional data returned from decoder.decode that is at the root level of general (for instance) such as id, name, ip_address etc. but I haven't been able to figure out how decoder.decode captures the remote_management data that goes a level deeper.

Any suggestions would be greatly appreciated!
Thanks!

William Jacobson
Autoclub, Costa Mesa

Example on how the data is decoded and referenced:
guard let result = try? decoder.decode(ComputerResults.self, from: data)
    else {
      exit(1)
    }
    
    for computer in result.results {
      print(computer.id,
            computer.general.name,
            computer.operatingSystem.version,
            computer.hardware.model)
    }
  }

Relevant part of the JSON ts decoding:

{"computer":
    {"general":
        {"id":75,
        "name":"MyComputer",
        "network_adapter_type":"IEEE80211",
        "mac_address":"BC:D0:74:4F:4C:D3",
        "alt_network_adapter_type":"Ethernet",
        "alt_mac_address":"36:98:ED:95:48:80",
        "ip_address":"XXX.XX.XX.X",
        "last_reported_ip":"XX.XX.XX.XXX",
        "last_reported_ip_v4":"XX.XX.XX.XX",
        "serial_number":"G07F2LM735",
        "udid":"76152E90-A402-5EEC-BC7B-7C4CF7EF7308",
        "jamf_version":"11.18.1-t1751293920915",
        "platform":"Mac",
        "asset_tag":"XXXXXX",
"remote_management":{"managed":true,"management_username":"deprecated","management_password_sha256":"deprecated"},
        "supervised":true,
        "mdm_capable":true,
        "mdm_capable_users":{"mdm_capable_user":"n904524"}
        ...
        }
    }
}

Hello

I'm early in my JAMF API & Swift learning curve. I have worked through the wonderful Swift/JAMF API tutorial at:
Part 2: https://www.jamf.com/blog/mac-admin-swift-jamf-pro-api-decoding-data-objects/

and I am now attempting to expand the reported data pulled from the JSON. I have no problem pulling additional data returned from decoder.decode that is at the root level of general (for instance) such as id, name, ip_address etc. but I haven't been able to figure out how decoder.decode captures the remote_management data that goes a level deeper.

Any suggestions would be greatly appreciated!
Thanks!

William Jacobson
Autoclub, Costa Mesa

Example on how the data is decoded and referenced:
guard let result = try? decoder.decode(ComputerResults.self, from: data)
    else {
      exit(1)
    }
    
    for computer in result.results {
      print(computer.id,
            computer.general.name,
            computer.operatingSystem.version,
            computer.hardware.model)
    }
  }

Relevant part of the JSON ts decoding:

{"computer":
    {"general":
        {"id":75,
        "name":"MyComputer",
        "network_adapter_type":"IEEE80211",
        "mac_address":"BC:D0:74:4F:4C:D3",
        "alt_network_adapter_type":"Ethernet",
        "alt_mac_address":"36:98:ED:95:48:80",
        "ip_address":"XXX.XX.XX.X",
        "last_reported_ip":"XX.XX.XX.XXX",
        "last_reported_ip_v4":"XX.XX.XX.XX",
        "serial_number":"G07F2LM735",
        "udid":"76152E90-A402-5EEC-BC7B-7C4CF7EF7308",
        "jamf_version":"11.18.1-t1751293920915",
        "platform":"Mac",
        "asset_tag":"XXXXXX",
"remote_management":{"managed":true,"management_username":"deprecated","management_password_sha256":"deprecated"},
        "supervised":true,
        "mdm_capable":true,
        "mdm_capable_users":{"mdm_capable_user":"n904524"}
        ...
        }
    }
}

To capture the remote_management data, you need to treat it as a nested object inside the general section of your JSON. Essentially, your decoding logic must recognize that remote_management isn’t at the root level but one level deeper. Once your model includes a representation for remote_management within general, you can access its properties through the general object of each computer. This way, the decoder can properly map and retrieve the managed status, username, and password fields from the JSON.


I found it extremely helpful to read this series of blog posts from Armin Briegal about this very topic. https://www.jamf.com/blog/mac-admin-swift-jamf-pro-api-command-line-tools/ I’ve also found supplementing my codable structs with something like https://quicktype.io/ also to be very helpful although they are not perfect and sometimes it requires a lot of adjustment. I would not suggest using JSONNull.


Reply