Home > Enterprise >  'list object' has no attribute 'name'
'list object' has no attribute 'name'

Time:11-10

here is my json file

"json": {
            "models": [
                {
                    "createTime": "2021-10-08T23",
                    "displayName": "po_model",
                    "etag": "AMEw9yNhi",
                    "labels": {
                        "purpose": "sqat"
                    },
                                           
                    "name": "projects/93057109696/locations/us-central1/models/1364172872674705408",
                    
                }
            ]
        }

and I want to retrieve the value from name field : 1364172872674705408 for which I am running below ansible playbook cmd

- name: set model ID
  set_fact : 
    model_id: "{{model_image_response.json.models.name.split('/')[5]}}"

but when I run this playbook i get this error

The error was: 'list object' has no attribute 'name'\

Any idea on this ? Thanks in Advance !! Cheers.

CodePudding user response:

As indicated by the [ that follows it models is a list, so it contains ordered items not named attributes. The first element in the list is a mapping, which does have the name attribute.

- name: set model ID
  set_fact: 
    model_id: "{{ model_image_response.json.models.0.name.split('/')[5] }}"

The added 0 is the index of the item in the list you want to access.

  • Related