0

I have the following json format string as below.

json_data_string = "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"range\":{\"@timestamp\":{\"time_zone\":\"+09:00\",\"gte\":\"2023-01-24T00:00:00.000Z\",\"lt\":\"2023-01-25T00:00:00.000Z\"}}},{\"term\":{\"serviceid.keyword\":{\"value\":\"430011397\"}}}]}},\"aggs\":{\"by_day\":{\"auto_date_histogram\":{\"field\":\"@timestamp\",\"minimum_interval\":\"minute\"},\"aggs\":{\"agg-type\":{\"terms\":{\"field\":\"nxlogtype.keyword\",\"size\":1000},\"aggs\":{\"my-sub-agg-name\":{\"avg\":{\"field\":\"size\"}}}}}}}}"

In this field, there is gte field which is the starting time. I would like to put this value by variable, not by the constant string as shown above.

For example, I want to generate many json format string by using the for-loop as below.

fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.strptime('2010-01-01 00:00:00', fmt)
d2 = datetime.strptime('2010-01-02 00:00:00', fmt)
minutesDiff = (d2 - d1).days * 24 * 60

for n in range(minutesDiff):
    print(json_data_string_variable.format(datetime.strptime(str(d1 + timedelta(minutes=n)),fmt)))

By using the iterative style, I think that I can generate the multiple json string but I don't have idea to insert variable format in the json_data_string. I have googled it and tried to use {} syntax in the json_data_string but it did not work.

How to achieve it?

Thanks.

2 Answers 2

1

You could use the older modulo formatting. Replace the gte field with %s:

json_data_string = "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"range\":{\"@timestamp\":{\"time_zone\":\"+09:00\",\"gte\":\"%s\",\"lt\":\"2023-01-25T00:00:00.000Z\"}}},{\"term\":{\"serviceid.keyword\":{\"value\":\"430011397\"}}}]}},\"aggs\":{\"by_day\":{\"auto_date_histogram\":{\"field\":\"@timestamp\",\"minimum_interval\":\"minute\"},\"aggs\":{\"agg-type\":{\"terms\":{\"field\":\"nxlogtype.keyword\",\"size\":1000},\"aggs\":{\"my-sub-agg-name\":{\"avg\":{\"field\":\"size\"}}}}}}}}"

Then use % to format the string

for n in range(minutesDiff):
    print(json_data_string % (datetime.strptime(str(d1 + timedelta(minutes=n)),fmt)))
Sign up to request clarification or add additional context in comments.

Comments

1

Convert the string representation of JSON to a Python dictionary. You can then access the 'gte' and 'lt' values as follows:

json_data_string = "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"range\":{\"@timestamp\":{\"time_zone\":\"+09:00\",\"gte\":\"2023-01-24T00:00:00.000Z\",\"lt\":\"2023-01-25T00:00:00.000Z\"}}},{\"term\":{\"serviceid.keyword\":{\"value\":\"430011397\"}}}]}},\"aggs\":{\"by_day\":{\"auto_date_histogram\":{\"field\":\"@timestamp\",\"minimum_interval\":\"minute\"},\"aggs\":{\"agg-type\":{\"terms\":{\"field\":\"nxlogtype.keyword\",\"size\":1000},\"aggs\":{\"my-sub-agg-name\":{\"avg\":{\"field\":\"size\"}}}}}}}}"

j = json.loads(json_data_string)

timestamp = j['query']['bool']['must'][0]['range']['@timestamp']

gte = timestamp['gte']
lt = timestamp['lt']

print(gte, lt)

Output:

2023-01-24T00:00:00.000Z 2023-01-25T00:00:00.000Z

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.