Jump to content

Accessing list by index in asset import config


Recommended Posts

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"
            }
        ]
    }
}

 

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

 

@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": ""
      }
    }
  ]
}

 

Link to comment
Share on other sites

  • 1 month later...

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}}"

 

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...