Gareth Cantrell Posted March 15, 2024 Posted March 15, 2024 When configuring an asset import, given the following data structure, how can I go about accessing the list by index? For example, I would like to get the "Value" attribute from the record with FieldId 3. Would the following work: {{.Object.FieldDataItems[2].Value}} ? { "Object": { "Status": 0, "FieldDataItems": [ { "FieldId": 1, "ObjectId": 3, "Value": "KSDFSERWRE", "ValueAsBit": null, "ValueAsDate": null, "ValueAsFloat": null, "ValueAsInt": null, "ValueAsString": "KSDFSERWRE" }, { "FieldId": 2, "ObjectId": 3, "Value": "2023-09-27T00:00:00", "ValueAsBit": null, "ValueAsDate": "2023-09-27T00:00:00+01:00", "ValueAsFloat": null, "ValueAsInt": null, "ValueAsString": null }, { "FieldId": 3, "ObjectId": 3, "Value": "KLSDLSAD", "ValueAsBit": null, "ValueAsDate": null, "ValueAsFloat": null, "ValueAsInt": null, "ValueAsString": "KLSDLSAD" } ] } }
SamS Posted March 15, 2024 Posted March 15, 2024 Hi @Gareth Cantrell, Which of the 14 (as of this writing) possible asset data sources our utility supports ( https://docs.hornbill.com/data-imports-guide/assets/configuration ) is this data coming from?
Gareth Cantrell Posted March 18, 2024 Author Posted March 18, 2024 Hi @SamS These are custom fields in Certero which we use to track things like building, stock room and floor number. They are retrieved with the following OData query snippet: ..., Object($expand=FieldDataItems), ...
SamS Posted March 19, 2024 Posted March 19, 2024 @Gareth Cantrell Under which parent object is "FieldDataItems" (or "Object" displaying)? "ComputerSystemInventory"? Other? Could you please respond with a FULL sample payload (obviously obscure the necessary data), and not just the branch of interest in this.
Gareth Cantrell Posted March 19, 2024 Author Posted March 19, 2024 @SamS The Hornbill Certero integration is rooted at ComputerSystem. Object is a NavigationProperty of ComputerSystem, similar to ComputerSystemInventory. The full OData API call to Certero is: ComputerSystem?$expand=ComputerSystemInventory( $expand=OperatingSystem,ADUser($select=AccountName) ), ComputerSystemProcessorInfo, Object( $select=LocationId,Status;$expand=FieldDataItems ), WindowsSystemBio And the result (with values removed) is: { "@odata.context": "https://xxx.certero.com/api/odata/$metadata#ComputerSystem(ComputerSystemInventory(OperatingSystem(*),ADUser(AccountName)),ComputerSystemProcessorInfo(*),Object(LocationId,Status,FieldDataItems(*)),WindowsSystemBio(*))", "value": [ { "ComputerSystemObjectId": "", "ClientProducts": "", "ClientType": "", "ClientTypes": "", "ComputerSystemConfigurationId": "", "Modules": "", "NodeObjectId": "", "OperatingSystem": "", "ComputerSystemInventory": { "ComputerSystemObjectId": "", "ADUserObjectId": "", "ComputerDomain": "", "ComputerName": "", "EndpointServerObjectId": "", "IpAddress": "", "LastUpdate": "", "MacAddress": "", "Manufacturer": "", "Model": "", "NetworkObjectId": "", "OperatingSystemId": "", "SubnetMask": "", "Username": "", "Version": "", "Virtual": false, "OperatingSystem": { "OperatingSystemId": "", "BuildNumber": "", "Caption": "", "Family": "", "MajorVersion": "", "MinorVersion": "", "ProcessorArchitecture": "", "ProductType": "", "ReturnedProductType": "", "ServicePackMajor": null, "ServicePackMinor": null, "SuiteMask": "", "SystemMetrics": "", "VersionString": "" }, "ADUser": { "AccountName": "" } }, "ComputerSystemProcessorInfo": { "ComputerSystemObjectId": "", "CacheSizeK": "", "ClockSpeed": "", "Cores": "", "CoresPerCpu": "", "HTCapable": true, "HTEnabled": true, "LMExt": true, "LogicalCpus": "", "Manufacturer": "", "Model": "", "PhysicalCpus": "", "VMXExt": true }, "Object": { "Status": "", "FieldDataItems": [ { "FieldId": "1", "ObjectId": "", "Value": "", "ValueAsBit": null, "ValueAsDate": null, "ValueAsFloat": null, "ValueAsInt": null, "ValueAsString": "", }, { "FieldId": "2", "ObjectId": "", "Value": "", "ValueAsBit": null, "ValueAsDate": "", "ValueAsFloat": null, "ValueAsInt": null, "ValueAsString": null, }, { "FieldId": "3", "ObjectId": "", "Value": "", "ValueAsBit": null, "ValueAsDate": null, "ValueAsFloat": null, "ValueAsInt": null, "ValueAsString": "", }, { "FieldId": "4", "ObjectId": "", "Value": "", "ValueAsBit": null, "ValueAsDate": null, "ValueAsFloat": null, "ValueAsInt": null, "ValueAsString": "", }, { "FieldId": "5", "ObjectId": "", "Value": "", "ValueAsBit": null, "ValueAsDate": null, "ValueAsFloat": null, "ValueAsInt": null, "ValueAsString": "", }, { "FieldId": "6", "ObjectId": "", "Value": "", "ValueAsBit": null, "ValueAsDate": null, "ValueAsFloat": null, "ValueAsInt": "", "ValueAsString": null, } ] }, "WindowsSystemBio": { "WindowsSystemObjectId": "", "BiosMode": "", "Manufacturer": "", "Name": "", "ProductName": "", "ReleaseDate": "", "SMBIOSBIOSVersion": "", "SMBIOSUUID": "", "SecureBootEnabled": false, "SerialNumber": "" } } ] }
Gareth Cantrell Posted May 14, 2024 Author Posted May 14, 2024 After many weeks I have finally figured out the answer to my original question, which I will document here for future reference: Given a list of items in a JSON structure, using the example above of .Object.FieldDataItems, you can match and retrieve a value using the following Go template: {{if .Object.FieldDataItems}} // only continue if this list is non-empty {{range $key,$value := .Object.FieldDataItems}} // iterate the list - $key is a zero-based index and $value is the object at index $key {{if eq $value.FieldId "3"}} // check if this is the object we want {{$value.Value}} // get the attribute we're interested in {{end}} {{end}} {{end}} In practice, this should be on a single line with escaped quotes in order to not break the JSON configuration file: "h_field": "{{if .Object.FieldDataItems}}{{range $key,$value := .Object.FieldDataItems}}{{if eq $value.FieldId \"3\"}}{{$value.Value}}{{end}}{{end}}{{end}}"
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now