How to find within all the objects of json in Python? - python

I have a json:
{
"total": 22,
"items": [
{
"id": 196,
"name": "Актау",
"code": "aktau",
"country": {
"name": "Казахстан",
"code": "kz"
}
},
{
"id": 167,
"name": "Актобе",
"code": "aktobe",
"country": {
"name": "Казахстан",
"code": "kz"
}
},
{
"id": 67,
"name": "Алматы",
"code": "almaty",
"country": {
"name": "Казахстан",
"code": "kz"
}
}
]
}
I need to print a "code" for every object within this json.
Can you please help?
What i'm asking is, can you somehow put all the objects in one brackets, like this?
json_response["items"][0, 1, 2]["country"]["code"]

You can use list comprehension:
result = [element["country"]["code"] for element in json_response["items"]]
Alternatively, you can use a normal for loop:
result = []
for element in json_response["items"]:
result.append(element["country"]["code"])

No you cannot do that kind of things but you can write a list comprehension that is close to what you want:
[item["country"]["code"] for item in json_response["items"]]
This is equivalent to:
[
json_response["country"]["code"][0],
json_response["country"]["code"][1],
...
]

You could do this:
J = {
"total": 22,
"items": [
{
"id": 196,
"name": "Актау",
"code": "aktau",
"country": {
"name": "Казахстан",
"code": "kz"
}
},
{
"id": 167,
"name": "Актобе",
"code": "aktobe",
"country": {
"name": "Казахстан",
"code": "kz"
}
},
{
"id": 67,
"name": "Алматы",
"code": "almaty",
"country": {
"name": "Казахстан",
"code": "kz"
}
}
]
}
print(*[j['code'] for j in J['items']])

Related

Splitting list in to N parts and load each part in to Azure function

I have a json list in the format below with 70 sources that I have to iterate and load url data in to the backend.
I already have the code and having issues deploying it as an Azure function to run every 10 minutes (timer trigger that has a 10 min timeout). In order to achieve this, I was thinking of iterating and splitting the list into 4 parts of approximately equal length and have azure function execute them within 10 min each. But, I am not able to split the list into equal parts and send subsequent list items to the function to load data. Please assist in achieving the following.
lst=[{
"input_type": "source_1",
"url_load": [
{
"url": "https:..."
"code": "site_1"
},
{
"url": "https:..."
"code": "site_2"
},
{
"url": "https:..."
"code": "site_3"
}]
},
{
"input_type": "source_2",
"url_load": [
{
"url": "https:..."
"code": "site_1"
},
{
"url": "https:..."
"code": "site_2"
},
{
"url": "https:..."
"code": "site_3"
}]
},
{
"input_type": "source_3",
"url_load": [
{
"url": "https:..."
"code": "site_1"
},
{
"url": "https:..."
"code": "site_2"
},
{
"url": "https:..."
"code": "site_3"
}]
},
{
"input_type": "source_4",
"url_load": [
{
"url": "https:..."
"code": "site_1"
},
{
"url": "https:..."
"code": "site_2"
},
{
"url": "https:..."
"code": "site_3"
}]
},.......
{
"input_type": "source_70",
"url_load": [
{
"url": "https:..."
"code": "site_1"
},
{
"url": "https:..."
"code": "site_2"
},
{
"url": "https:..."
"code": "site_3"
}]
}]
for i in range(0,70,5)
if len(lst[:i])>0:
func.load(lst[0:i])
#"schedule": "0 */10 2,3 * * *"
dt=datetime.now()
ten_min_segment = math.floor(dt.minute/10)
hour_seg - dt.hour - 3
ordinal = hour_seg *6 + ten_min_segment
url_list = lst[ordinal]
function.load(url_list)

How to erase/delete curly brackets/braces from a dictionary?

My JSON response looks like this. I want to remove curly brackets (with ** around it) so I can get the values in the card key. Can I do that or is it gonna mess up the entire Dictionary? If so, I want to assign key-value before curly brackets (with ** around it ).
Hope someone can help me with this and if you can give me a further explanation about a dictionary in python I would be thrilled!
[
**{**
"board": {
"id": "5f2106f0a188d073ebf3604b",
"name": "TrAPI_test",
"shortLink": "OIeEN1vG"
},
"card": {
"id": "5f236a13a64ee90e7ef95341",
"idShort": 3,
"name": "task3",
"shortLink": "WNHiHWxh"
},
"idMember": "5e1d96663a14c86d44d0edc4",
"member": {
"id": "5e1d96663a14c86d44d0edc4",
"name": "Zorigt"
}
**}**,
{
"board": {
"id": "5f2106f0a188d073ebf3604b",
"name": "TrAPI_test",
"shortLink": "OIeEN1vG"
},
"card": {
"id": "5f236a13a64ee90e7ef95341",
"idShort": 3,
"name": "task3",
"shortLink": "WNHiHWxh"
},
"list": {
"id": "5f22161e221bea80b90d96ad",
"name": "SprintTask"
}
}
]
I was able to get it like this because it was a list of multiple dictionaries all along :))
{
"0": {
"board": {
"id": "5f2106f0a188d073ebf3604b",
"name": "TrAPI_test",
"shortLink": "OIeEN1vG"
},
"card": {
"id": "5f236a13a64ee90e7ef95341",
"idShort": 3,
"name": "task3",
"shortLink": "WNHiHWxh"
},
"idMember": "5e1d96663a14c86d44d0edc4",
"member": {
"id": "5e1d96663a14c86d44d0edc4",
"name": "Zorigt"
}
},
"1": {
"board": {
"id": "5f2106f0a188d073ebf3604b",
"name": "TrAPI_test",
"shortLink": "OIeEN1vG"
},
"card": {
"id": "5f236a13a64ee90e7ef95341",
"idShort": 3,
"name": "task3",
"shortLink": "WNHiHWxh"
},
"list": {
"id": "5f22161e221bea80b90d96ad",
"name": "SprintTask"
}
}
}
Use this
Dict_convert= {}
for idx, val in enumerate(List):
Dict_convert[idx] = val

Python Script to convert multiple json files in to single csv

{
"type": "Data",
"version": "1.0",
"box": {
"identifier": "abcdef",
"serial": "12345678"
},
"payload": {
"Type": "EL",
"Version": "1",
"Result": "Successful",
"Reference": null,
"Box": {
"Identifier": "abcdef",
"Serial": "12345678"
},
"Configuration": {
"EL": "1"
},
"vent": [
{
"ventType": "Arm",
"Timestamp": "2020-03-18T12:17:04+10:00",
"Parameters": [
{
"Name": "Arm",
"Value": "LT"
},
{
"Name": "Status",
"Value": "LD"
}
]
},
{
"ventType": "Arm",
"Timestamp": "2020-03-18T12:17:24+10:00",
"Parameters": [
{
"Name": "Arm",
"Value": "LT"
},
{
"Name": "Status",
"Value": "LD"
}
]
},
{
"EventType": "TimeUpdateCompleted",
"Timestamp": "2020-03-18T02:23:21.2979668Z",
"Parameters": [
{
"Name": "ActualAdjustment",
"Value": "PT0S"
},
{
"Name": "CorrectionOffset",
"Value": "PT0S"
},
{
"Name": "Latency",
"Value": "PT0.2423996S"
}
]
}
]
}
}
If you're looking to transfer information from a JSON file to a CSV, then you can use the following code to read in a JSON file into a dictionary in Python:
import json
with open('data.txt') as json_file:
data_dict = json.load(json_file)
You could then convert this dictionary into a list with either data_dict.items() or data_dict.values().
Then you just need to write this list to a CSV file which you can easily do by just looping through the list.

Fast way of adding fields to a nested dict

I need a help with improving my code.
I've got a nested dict with many levels:
{
"11": {
"FacLC": {
"immty": [
"in_mm",
"in_mm"
],
"moood": [
"in_oo",
"in_oo"
]
}
},
"22": {
"FacLC": {
"immty": [
"in_mm",
"in_mm",
"in_mm"
]
}
}
}
And I want to add additional fields on every level, so my output looks like this:
[
{
"id": "",
"name": "11",
"general": [
{
"id": "",
"name": "FacLC",
"specifics": [
{
"id": "",
"name": "immty",
"characteristics": [
{
"id": "",
"name": "in_mm"
},
{
"id": "",
"name": "in_mm"
}
]
},
{
"id": "",
"name": "moood",
"characteristics": [
{
"id": "",
"name": "in_oo"
},
{
"id": "",
"name": "in_oo"
}
]
}
]
}
]
},
{
"id": "",
"name": "22",
"general": [
{
"id": "",
"name": "FacLC",
"specifics": [
{
"id": "",
"name": "immty",
"characteristics": [
{
"id": "",
"name": "in_mm"
},
{
"id": "",
"name": "in_mm"
},
{
"id": "",
"name": "in_mm"
}
]
}
]
}
]
}
]
I managed to write a 4-times nested for loop, what I find inefficient and inelegant:
for main_name, general in my_dict.items():
generals = []
for general_name, specific in general.items():
specifics = []
for specific_name, characteristics in specific.items():
characteristics_dicts = []
for characteristic in characteristics:
characteristics_dicts.append({
"id": "",
"name": characteristic,
})
specifics.append({
"id": "",
"name": specific_name,
"characteristics": characteristics_dicts,
})
generals.append({
"id": "",
"name": general_name,
"specifics": specifics,
})
my_new_dict.append({
"id": "",
"name": main_name,
"general": generals,
})
I am wondering if there is more compact and efficient solution.
In the past I created a function to do it. Basically you call this function everytime that you need to add new fields to a nested dict, independently on how many levels this nested dict have. You only have to inform the 'full path' , that I called the 'key_map'.
Like ['node1','node1a','node1apart3']
def insert_value_using_map(_nodes_list_to_be_appended, _keys_map, _value_to_be_inserted):
for _key in _keys_map[:-1]:
_nodes_list_to_be_appended = _nodes_list_to_be_appended.setdefault(_key, {})
_nodes_list_to_be_appended[_keys_map[-1]] = _value_to_be_inserted

Python - Find value anywhere within JSON and return location

In Python I'm currently working with a very large JSON file with some deep dictionaries and arrays. I'm having an issue where it's not constant. For example that's below, it's essentially countries, with regions/states, cities, and suburbs. The issue is that if there is only one suburb, it'll return a dictionary, though if there's more than one, it's a array with a dictionary making me have to add another line of code to go deeper. Sure, can ifelse/for it, but this is only a very small portion of the inconstancy and it's just not proper going ifelse all the time.
What I'd like to do is simply search anything within Belgium for the dictionary entry "code": "8400" and return it's location within the JSON file. What would be my best approach in order to do something like this? Thanks!
***SNIP***
{
"code": "BE",
"name": "Belgium",
"regions": {
"region": [
{
"code": "45",
"name": "Flanders",
"places": {
"place": [
{
"code": "1790",
"name": "Affligem"
},
{
"code": "8570",
"name": "Anzegem"
},
{
"code": "8630",
"name": "Diksmuide"
},
{
"code": "9600",
"name": "Ronse"
}
]
},
"subregions": {
"subregion": [
{
"code": "46",
"name": "Coast",
"places": {
"place": [
{
"code": "8300",
"name": "Knokke-Heist"
},
{
"code": "8400",
"name": "Oostende",
"subplaces": {
"subplace": {
"code": "8450",
"name": "Bredene"
}
}
},
{
"code": "8420",
"name": "De Haan"
},
{
"code": "8430",
"name": "Middelkerke"
},
{
"code": "8434",
"name": "Westende-Bad"
},
{
"code": "8490",
"name": "Jabbeke"
},
{
"code": "8660",
"name": "De Panne"
},
{
"code": "8670",
"name": "Oostduinkerke"
}
]
}
},
{
"code": "47",
"name": "Cities",
"places": {
"place": [
{
"code": "1000",
"name": "Brussels"
},
{
"code": "2000",
"name": "Antwerp"
},
{
"code": "8000",
"name": "Bruges"
},
{
"code": "8340",
"name": "Damme"
},
{
"code": "9000",
"name": "Gent"
}
]
}
},
{
"code": "48",
"name": "Interior",
"places": {
"place": [
{
"code": "2260",
"name": "Westerlo"
},
{
"code": "2400",
"name": "Mol"
},
{
"code": "2590",
"name": "Berlaar"
},
{
"code": "8500",
"name": "Kortrijk",
"subplaces": {
"subplace": {
"code": "8940",
"name": "Wervik"
}
}
},
{
"code": "8610",
"name": "Handzame"
},
{
"code": "8755",
"name": "Ruiselede"
},
{
"code": "8900",
"name": "Ieper"
},
{
"code": "8970",
"name": "Poperinge"
}
]
}
},
EDIT:
I was asked to show how I'm currently getting through this JSON file. Root is a dictionary containing numbers that equal the city/suburb I'm trying to search for. It doesn't define whether it is a city or suburb before hand. Below is my lazyly coded search while I was trying to learn how to dig through this JSON file, until I realized how complicated it was getting and got a bit stuck.
SNIP
for k in dataDict['countries']['country']:
if k['code'] == root['country']:
for y in k['regions']['region']['places']['place']:
if y['code'] == root['place']:
city = y['name']
else:
try:
for p in y['subplaces']['subplace']:
if p['code'] == root['place']:
city = p['name']
except:
pass
If I understand well, each dictionary has the following structure:
{"code": # some int
"name": # some str
none / "country" / "place" / whatever # some dict or list
You can write a recursive function that handle one and only one dict:
def foo(my_dict):
if my_dict['code'] == root['place']:
city = my_dict['name']
elif "country" in my_dict:
city = foo(my_dict['country'])
elif "place" in my_dict:
#
# and so on...
else:
city = None
return city
Hope this example will help you.

Categories

Resources