Smruti Sahoo
Smruti Sahoo

Reputation: 169

I am unable to create dictionary

I am unable to create a dictionary whose structure is like :

"aggs": {
        "4": {
            "date_histogram": {
                "field": "@timestamp",
                "interval": "1m",
                "time_zone": "Asia/Kolkata",
                "min_doc_count": 1,
            },
            "aggs": {
                "5": {
                    "range": {
                        "field": "system.core.idle.pct",
                        "ranges": [{"from": 0, "to": 0.001}, {"from": 0.001, "to": 1}],
                        "keyed": true,
                    },
                    "aggs": {
                        "2": {"max": {"field": "system.core.system.pct"}},
                        "3": {"max": {"field": "system.core.idle.pct"}},
                    },
                }
            },
        }
    }

Mostly I am getting the issue in creating aggs:5 inside aggs:4 and then looping it. I need to do it for multiple aggs. the number of agg id can be upto 1000 also.

I am trying to derive it from the dictionary:

"aggs": [
        {"id": "1", "enabled": true, "type": "count", "schema": "metric", "params": {}},
        {
            "id": "2",
            "enabled": true,
            "type": "max",
            "schema": "metric",
            "params": {"field": "system.core.system.pct", "customLabel": "system"},
        },
        {
            "id": "3",
            "enabled": true,
            "type": "max",
            "schema": "metric",
            "params": {"field": "system.core.idle.pct", "customLabel": "idle"},
        },
        {
            "id": "4",
            "enabled": true,
            "type": "date_histogram",
            "schema": "bucket",
            "params": {
                "field": "@timestamp",
                "interval": "m",
                "customInterval": "2h",
                "min_doc_count": 1,
                "extended_bounds": {},
            },
        },
        {
            "id": "5",
            "enabled": true,
            "type": "range",
            "schema": "bucket",
            "params": {
                "field": "system.core.idle.pct",
                "ranges": [{"from": 0, "to": 0.001}, {"from": 0.001, "to": 1}],
            },
        },
    ]

Can anyone show the code how to execute it. Basically I am failing in creating nested dictionary.

Upvotes: 0

Views: 64

Answers (2)

Amir saleem
Amir saleem

Reputation: 1496

add whole structure in {}, and replace true to True.

{"aggs": {
        "4": {
            "date_histogram": {
                "field": "@timestamp",
                "interval": "1m",
                "time_zone": "Asia/Kolkata",
                "min_doc_count": 1,
            },
            "aggs": {
                "5": {
                    "range": {
                        "field": "system.core.idle.pct",
                        "ranges": [{"from": 0, "to": 0.001}, {"from": 0.001, "to": 1}],
                        "keyed": True,
                    },
                    "aggs": {
                        "2": {"max": {"field": "system.core.system.pct"}},
                        "3": {"max": {"field": "system.core.idle.pct"}},
                    },
                }
            },
        }
    }}
{'aggs': {'4': {'date_histogram': {'field': '@timestamp',
    'interval': '1m',
    'time_zone': 'Asia/Kolkata',
    'min_doc_count': 1},
   'aggs': {'5': {'range': {'field': 'system.core.idle.pct',
      'ranges': [{'from': 0, 'to': 0.001}, {'from': 0.001, 'to': 1}],
      'keyed': True},
     'aggs': {'2': {'max': {'field': 'system.core.system.pct'}},
      '3': {'max': {'field': 'system.core.idle.pct'}}}}}}}}

Upvotes: 0

Subhajit Das
Subhajit Das

Reputation: 106

It looks like you are using JSON data. You can see this link https://realpython.com/python-json/. You can use the inbuilt JSON lib to covert the whole thing in a dict Basically something like this

json_string = """
{
    "researcher": {
        "name": "Ford Prefect",
        "species": "Betelgeusian",
        "relatives": [
            {
                "name": "Zaphod Beeblebrox",
                "species": "Betelgeusian"
            }
        ]
    }
}
"""
data = json.loads(json_string)

Upvotes: 1

Related Questions