In the following data structure:
[
{
"id": 28,
"country": "Brazil",
"country_code": "BR",
"country_population": 201103330,
"province": "",
"last_updated": "2020-04-03T01:40:00.724616Z",
"coordinates": {
"latitude": "-14.235",
"longitude": "-51.9253"
},
"latest": {
"confirmed": 8044,
"deaths": 324,
"recovered": 0
},
"timelines": {
"confirmed": {
"latest": 8044,
"timeline": {
"2020-01-22T00:00:00Z": 0,
"2020-01-23T00:00:00Z": 0,
"2020-01-24T00:00:00Z": 0,
}
},
"deaths": {
"latest": 324,
"timeline": {
"2020-01-22T00:00:00Z": 0,
"2020-01-23T00:00:00Z": 0,
"2020-01-24T00:00:00Z": 0,
}
},
"recovered": {
"latest": 0,
"timeline": {}
}
}
}
]
How do I get "timeline" items, from "timelines" key?
You should provide at least a piece of code of what you tried for now..
d = [
{
"id": 28,
"country": "Brazil",
"country_code": "BR",
"country_population": 201103330,
"province": "",
"last_updated": "2020-04-03T01:40:00.724616Z",
"coordinates": {
"latitude": "-14.235",
"longitude": "-51.9253"
},
"latest": {
"confirmed": 8044,
"deaths": 324,
"recovered": 0
},
"timelines": {
"confirmed": {
"latest": 8044,
"timeline": {
"2020-01-22T00:00:00Z": 0,
"2020-01-23T00:00:00Z": 0,
"2020-01-24T00:00:00Z": 0,
}
},
"deaths": {
"latest": 324,
"timeline": {
"2020-01-22T00:00:00Z": 0,
"2020-01-23T00:00:00Z": 0,
"2020-01-24T00:00:00Z": 0,
}
},
"recovered": {
"latest": 0,
"timeline": {}
}
}
}
]
print(d[0]["timelines"]["confirmed"]["timeline"])
By the way :
"timeline": {
"2020-01-22T00:00:00Z": 0,
"2020-01-23T00:00:00Z": 0,
"2020-01-24T00:00:00Z": 0,
}
Looks weird for me does timeline should be an array instead of a object ?
print([[i, data[0]['timelines'][i]['timeline']] for i in data[0]['timelines']])
Your JSON does indeed have an issue
"JSONDecodeError: Expecting property name enclosed in double quotes: line 24 column 9 (char 558)"
Which in turn is your timeline as posted above
"timeline": {
"2020-01-22T00:00:00Z": 0,
"2020-01-23T00:00:00Z": 0, <----
"2020-01-24T00:00:00Z": 0,
JSONs have many formatting issues, and you may have to develop your own method of reading them if they are out of the general norm, I've had to do this a few times.
import json
x = """[{
"id": 28,
"country": "Brazil",
"country_code": "BR",
"country_population": 201103330,
"province": "",
"last_updated": "2020-04-03T01:40:00.724616Z",
"coordinates": {
"latitude": "-14.235",
"longitude": "-51.9253"
},
"latest": {
"confirmed": 8044,
"deaths": 324,
"recovered": 0
},
"timelines": {
"confirmed": {
"latest": 8044,
"timeline": {
"2020-01-22T00:00:00Z": 0,
"2020-01-23T00:00:00Z": 0,
"2020-01-24T00:00:00Z": 0,
}
},
"deaths": {
"latest": 324,
"timeline": {
"2020-01-22T00:00:00Z": 0,
"2020-01-23T00:00:00Z": 0,
"2020-01-24T00:00:00Z": 0,
}
},
"recovered": {
"latest": 0,
"timeline": {}
}
}
}]"""
y = json.loads(x)
print(y)
Related
I have this json file loaded in Python with json.loads('myfile.json'):
[
{
"cart": {
"items": {
"3154ba405e5c5a22bbdf9bf1": {
"item": {
"_id": "3154ba405e5c5a22bbdf9bf1",
"title": "Drink alla cannella",
"price": 5.65,
"__v": 0
},
"qty": 1,
"price": 5.65
}
},
"totalQty": 1,
"totalPrice": 5.65
}
},
{
"cart": {
"items": {
"6214ba405e4c5a31bbdf9ad7": {
"item": {
"_id": "6214ba405e4c5a31bbdf9ad7",
"title": "Drink alla menta",
"price": 5.65,
"__v": 0
},
"qty": 2,
"price": 11.3
}
},
"totalQty": 2,
"totalPrice": 11.3
}
}
]
How I can access to both totalQty and totalPrice fields at same time and sum them?
How I can access to both Title fields to print it?
Let's assume that you have the JSON data available as a string then:
jdata = '''
[
{
"cart": {
"items": {
"3154ba405e5c5a22bbdf9bf1": {
"item": {
"_id": "3154ba405e5c5a22bbdf9bf1",
"title": "Drink alla cannella",
"price": 5.65,
"__v": 0
},
"qty": 1,
"price": 5.65
}
},
"totalQty": 1,
"totalPrice": 5.65
}
},
{
"cart": {
"items": {
"6214ba405e4c5a31bbdf9ad7": {
"item": {
"_id": "6214ba405e4c5a31bbdf9ad7",
"title": "Drink alla menta",
"price": 5.65,
"__v": 0
},
"qty": 2,
"price": 11.3
}
},
"totalQty": 2,
"totalPrice": 11.3
}
}
]
'''
totalQty = 0
totalPrice = 0
for d in json.loads(jdata):
c = d['cart']
totalQty += c['totalQty']
totalPrice += c['totalPrice']
for sd in c['items'].values():
print(sd['item']['title'])
print(f'{totalQty:d}', f'{totalPrice:.2f}')
Output:
3 16.95
Note:
I suspect that what you really want to do is multiply those two values
I have dictionary which is below
{
"aggregations": {
"A": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{ "key": "ADL", "doc_count": 1 },
{ "key": "SDD", "doc_count": 1 },
{ "key": "JJD", "doc_count": 1 }
]
},
"B": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{ "key": "ABC", "doc_count": 1 },
{ "key": "CDE", "doc_count": 1 },
{ "key": "FGH", "doc_count": 1 }
]
},
"C": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{ "key": "XYX", "doc_count": 1 },
{ "key": "NXS", "doc_count": 1 }
]
}
}
}
aggregations.keys will be aggregationfilters.fieldName
aggregations.buckets.key will be aggregationfilters.values.title
aggregationfilters.values.paragraph is null everytime
aggregations.buckets.doc_count will be aggregationfilters.values.count
Basically I need to extract aggregations.keys and aggregations.bucket values and put into different dictionary.
Need to write a general code structure to do that.
I cannot do with .pop(rename) the dictioanry
My expected out
{
"aggregationfilters": [
{
"name": "ABC",
"fieldName": "A",
"values": [
{ "title": "ADL", "paragraph": null, "count": 1 },
{ "title": "SDD", "paragraph": null, "count": 1 },
{ "title": "JJD", "paragraph": null, "count": 1 }
]
}, {
"name": "CDE",
"fieldName": "B",
"values": [
{ "title": "ABC", "paragraph": null, "count": 1 },
{ "title": "CDE", "paragraph": null, "count": 1 },
{ "title": "FGH", "paragraph": null, "count": 1 }
]
}, {
"name": "FGH",
"fieldName": "C",
"values": [
{ "title": "XYX", "paragraph": null, "count": 1 },
{ "title": "NXS", "paragraph": null, "count": 1 }
]
}
]
}
Well, this works, but even with my best effort this still doesn't look that clean.
import json
source = {
"aggregations": {
"A": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{"key": "ADL", "doc_count": 1},
{"key": "SDD", "doc_count": 1},
{"key": "JJD", "doc_count": 1},
],
},
"B": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{"key": "ABC", "doc_count": 1},
{"key": "CDE", "doc_count": 1},
{"key": "FGH", "doc_count": 1},
],
},
"C": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [{"key": "XYX", "doc_count": 1}, {"key": "NXS", "doc_count": 1}],
},
}
}
convert_map = {
"buckets": "values",
"doc_count": "count",
"key": "title",
}
remove_map = {"sum_other_doc_count", "doc_count_error_upper_bound"}
add_map = {"name": "Changed VAL_", "fieldName": "VAL_"}
def converting_generator(
source_: dict, convert_map_: dict, remove_map_: set, add_map_: dict
):
working_dict = {k: v for k, v in source_.items()}
variable_identifier = "VAL_"
for key, inner_dic in working_dict.items():
inner_dic: dict
for rm_key in remove_map_:
try:
inner_dic.pop(rm_key)
except KeyError:
pass
for add_key, add_val in add_map_.items():
inner_dic[add_key] = add_val.replace(variable_identifier, key)
dumped = json.dumps(inner_dic, indent=2)
for original, target in convert_map_.items():
dumped = dumped.replace(original, target)
yield json.loads(dumped)
converted = {
"aggregation_filters": list(
converting_generator(source["aggregations"], convert_map, remove_map, add_map)
)
}
for inner_dict in converted["aggregation_filters"]:
for even_inner_dict in inner_dict["values"]:
even_inner_dict["paragraph"] = None
print(json.dumps(converted, indent=2))
Output:
{
"aggregation_filters": [
{
"values": [
{
"title": "ADL",
"count": 1,
"paragraph": null
},
{
"title": "SDD",
"count": 1,
"paragraph": null
},
{
"title": "JJD",
"count": 1,
"paragraph": null
}
],
"name": "Changed A",
"fieldName": "A"
},
{
"values": [
{
"title": "ABC",
"count": 1,
"paragraph": null
},
{
"title": "CDE",
"count": 1,
"paragraph": null
},
{
"title": "FGH",
"count": 1,
"paragraph": null
}
],
"name": "Changed B",
"fieldName": "B"
},
{
"values": [
{
"title": "XYX",
"count": 1,
"paragraph": null
},
{
"title": "NXS",
"count": 1,
"paragraph": null
}
],
"name": "Changed C",
"fieldName": "C"
}
]
}
Always show your code, would be nice if that's a working one - to show that you've put at least that worth of the effort on your problem.
I don't bother it as this feels like puzzle solving, but others may not.
Hello I have the following problem, whenever I aggregate data, the aggregations and to be more exact the date_histogram is always different. It starts with pretty much random date.
I am using elasticpy and my query looks like this before executing. Note that I am using python datetime objects to get a "real" results. I had some problems with other formats.
{
"query": {
"bool": {
"filter": [
{
"range": {
"original_date": {
"gte": datetime.datetime(2020, 2, 13, 0, 0),
"lte": datetime.datetime(2020, 2, 15, 23, 0),
}
}
}
],
"must": [
{
"query_string": {
"query": "whatever string"
}
}
],
}
},
"aggs": {
"docs_histogram": {
"date_histogram": {
"field": "original_date",
"interval": "hour",
"time_zone": "EET",
},
... (other aggs)
},
},
}
The date histogram should be in this range: 2020-02-13 00:00:00 - 2020-02-15 23:00:00 But look at the output's start and end. It starts 1 day later and ends same day 18:00 ??
"buckets": [
{
"key_as_string": "2020-02-14T00:00:00.000+02:00",
"key": 1581631200000,
"doc_count": 1,
"source_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [{"key": "WhateverKey", "doc_count": 1}],
},
},
...
{
"key_as_string": "2020-02-14T18:00:00.000+02:00",
"key": 1581696000000,
"doc_count": 1,
"source_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [{"key": "WhateverKey2", "doc_count": 1}],
},
},
]
How would I get the names of the keys, for example [800, 801] (the key names are unknown) with objectpath.
It is easy in jmespath: keys(#).
"groups": {
"800": {
"short_name": "22",
"oname": "11",
"group": 8,
"title": "SS",
"name": "33",
"onames": [""],
"alt_name": False,
"waytype": 1,
"multiple": 1,
"primary": 1
},
"801": {
"short_name": "ss",
"oname": "zz",
"group": 8,
"title": "ss",
"name": "bbb",
"onames": [""],
"alt_name": False,
"waytype": 1,
"multiple": 1,
"primary": 0
},
let your object is assigned to name variable
const name = { "groups": {
"800": {
"short_name": "22",
"oname": "11",
"group": 8,
"title": "SS",
"name": "33",
"onames": [""],
"alt_name": false,
"waytype": 1,
"multiple": 1,
"primary": 1
},
"801": {
"short_name": "ss",
"oname": "zz",
"group": 8,
"title": "ss",
"name": "bbb",
"onames": [""],
"alt_name": false,
"waytype": 1,
"multiple": 1,
"primary": 0
} } }
Use for loop to get the key name as
for(var num in name.groups) {
console.log(num);
}
and to get the values of key
for(var num in name.groups) {
console.log(name.groups[num]);
}
This is my JSON file and I want to sum all values of number key. How can i do that with Python?
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 7,
"max_score": 1.0,
"hits": [{
"_index": "test",
"_type": "json",
"_id": "5878",
"_score": 1.0,
"_source": {
"data_type": "click",
"number": 1,
"date": "1397/05/14",
"host_id": "1231"
}
}, {
"_index": "test",
"_type": "json",
"_id": "1548",
"_score": 1.0,
"_source": {
"data_type": "click",
"number": 1,
"date": "1397/05/14",
"host_id": "1231"
}
}, {
"_index": "test",
"_type": "json",
"_id": "2751",
"_score": 1.0,
"_source": {
"data_type": "click",
"number": 1,
"date": "1397/05/14",
"host_id": "1231"
}
}, {
"_index": "test",
"_type": "json",
"_id": "8363",
"_score": 1.0,
"_source": {
"data_type": "click",
"number": 1,
"date": "1397/05/14",
"host_id": "1231"
}
}, {
"_index": "test",
"_type": "json",
"_id": "551",
"_score": 1.0,
"_source": {
"data_type": "click",
"number": 1,
"date": "1397/05/14",
"host_id": "1231"
}
}, {
"_index": "test",
"_type": "json",
"_id": "2195",
"_score": 1.0,
"_source": {
"data_type": "click",
"number": 1,
"date": "1397/05/14",
"host_id": "1231"
}
}, {
"_index": "test",
"_type": "json",
"_id": "2990",
"_score": 1.0,
"_source": {
"data_type": "click",
"number": 1,
"date": "1397/05/14",
"host_id": "1231"
}
}]
}
}
import json
data = '{"took": 0, "timed_out": false, "_shards": {"total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": {"total": 7, "max_score": 1.0, "hits": [{"_index": "test", "_type": "json", "_id": "5878", "_score": 1.0, "_source": {"data_type": "click", "number": 1, "date": "1397/05/14", "host_id": "1231"} }, {"_index": "test", "_type": "json", "_id": "1548", "_score": 1.0, "_source": {"data_type": "click", "number": 1, "date": "1397/05/14", "host_id": "1231"} }, {"_index": "test", "_type": "json", "_id": "2751", "_score": 1.0, "_source": {"data_type": "click", "number": 1, "date": "1397/05/14", "host_id": "1231"} }, {"_index": "test", "_type": "json", "_id": "8363", "_score": 1.0, "_source": {"data_type": "click", "number": 1, "date": "1397/05/14", "host_id": "1231"} }, {"_index": "test", "_type": "json", "_id": "551", "_score": 1.0, "_source": {"data_type": "click", "number": 1, "date": "1397/05/14", "host_id": "1231"} }, {"_index": "test", "_type": "json", "_id": "2195", "_score": 1.0, "_source": {"data_type": "click", "number": 1, "date": "1397/05/14", "host_id": "1231"} }, {"_index": "test", "_type": "json", "_id": "2990", "_score": 1.0, "_source": {"data_type": "click", "number": 1, "date": "1397/05/14", "host_id": "1231"} }] } } '
myk = json.loads(data)
count = 0
target = myk['hits']['hits']
for l in target:
for k in l:
if k == "_source":
count += l[k]['number']
print(count)
output:
7
>>>
Loading data from a json file
import json
with open('data.json') as f:
data = json.load(f)
count = 0
target = data['hits']['hits']
for l in target:
for k in l:
if k == "_source":
count += l[k]['number']
print(count)
7
>>>