0

I am trying to grab the value green from the below JSON data dictionary.

The API endpoint located at http://localhost:9200/api/status give the below data:

{
   "name":"prod01",
   "uuid":"3430c40-e786-4325-bc48-e0a096956000",
   "version":{
      "number":"7.17.0",
      "build_hash":"60a9838d21b6420bbdb5a4d07099111b74c68ceb",
      "build_number":46534,
      "build_snapshot":false
   },
   "status":{
      "overall":{
         "since":"2023-02-13T22:47:05.381Z",
         "state":"green",
         "title":"Green",
         "nickname":"Looking good",
         "icon":"success",
         "uiColor":"secondary"
      },
      "statuses":[
         {
            "id":"core:elasticsearch@7.17.0",
            "message":"Elasticsearch is available",
            "since":"2023-02-13T22:47:05.381Z",
            "state":"green",
            "icon":"success",
            "uiColor":"secondary"
         },
         {
            "id":"core:savedObjects@7.17.0",
            "message":"SavedObjects service has completed migrations and is available",
            "since":"2023-02-13T22:47:05.381Z",
            "state":"green",
            "icon":"success",
            "uiColor":"secondary"
         }
      ]
   }
}

And the test.sls file doing the job is:

{% set json_data = salt.cp.get_url('http://localhost:9200/api/status', dest=None) | load_json %}

{% for key, value in json_data.items() %}
{% if value['state'] == 'green' %}
{{ key }}: {{ value }} (found)
{% else %}
{{ key }}: {{ value }}
{% endif %}
{% endfor %}

Executing it, I get the error:

Rendering SLS 'base:svr.salt.dev.test' failed: Jinja variable 'str object' has no attribute 'state'

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • 1
    Hi. the JSON sample posted in question seems incomplete and its not possible to reproduce what you are trying to do. That said, you can try using [HTTP module](https://docs.saltproject.io/en/latest/topics/tutorials/http.html) which will give access to a dict containing the JSON output. – seshadri_c Feb 14 '23 at 07:43
  • Does this answer your question? [Get key by value in dictionary](https://stackoverflow.com/questions/8023306/get-key-by-value-in-dictionary) – OrangeDog Feb 15 '23 at 22:11

1 Answers1

0

You are looping on all the key-value pairs of the object json_data, with json_data.items(), so, you do not have a my_dictionary['state'] anymore, what you have is a key which will be state and its value will be green.

This said, your {"state": "green"} is not in the root of your dictionary, so you will never find any key-value pair matching what you need.

What you can do, though is:

{% if load_json.status.overall.state == 'green' -%}
  state: {{ load_json.status.overall.nickname }}
{% endif %}

Which would yield:

state: Looking good
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • I made change as per your suggestion: {% set json_data = salt.cp.get_url('http://localhost:9200/api/status', dest=None) | load_json %} {% if json_data.status.overall.state == 'green' %} {{ json_data.status.overall.nickname }} {% endif %} but got below error : Data failed to compile:Rendering SLS 'base::svr.salt.dev.test3' does not render to a dictionary. Error when rendering state with contents: Looking good – kevin voyce Feb 15 '23 at 04:03
  • As the message clearly points out, it seems a `sls` file in Salt needs to be a YAML, is, see the updated answer to have a valid YAML. – β.εηοιτ.βε Feb 15 '23 at 19:55