How to pull specific key from this nested dictionary?

2024/10/6 8:23:46
{"newData": [{"env1": [{"sins": [{"host": "test.com","deployTime": "2015-07-23 11:54 AM",…}],"name": “hello”}, {"env1": [{"sins": [{"host": "test.com","deployTime": "2015-12-16 05:23 PM",…}],"name": "hello"

I am trying to pull the 'host' and the 'name' from this nested dictionary.

I only know how to get 'name' and append to a list, but I want to append both name and 'host'.

Currently I am doing

list=[]
for row in my_dict['newData']:list.append(row)
for i in list:print i['name']
Answer

Your dictionary is utterly broken. Please post at least an upright structure. Assuming it should look like this:

test= {"newData": [{"env1": [{"sins": [{"host": "test.com","deployTime": "2015-07-23 11:54 AM",}],"name": "hello"}]}, {"env1": [{"sins": [{"host": "test.com","deployTime": "2015-12-16 05:23 PM",}],"name": "hello"}]}]}

then this:

print([element['env1'][0]['sins'][0]['host'] + ' ' \+ element['env1'][0]['name'] for element in test['newData']])

would yield:

['test.com hello', 'test.com hello']
https://en.xdnf.cn/q/118970.html

Related Q&A

Dropping cell if it is NaN in a Dataframe in python

I have a dataframe like this.Project 4 Project1 Project2 Project3 0 NaN laptio AB NaN 1 NaN windows ten NaN 0 one NaN NaN 1 …

How can I iterate through excel files sheets and insert formula in Python?

I get this error TypeError: Workbook object is not subscriptablewhen i run this code import xlsxwriter from openpyxl import load_workbookin_folder = rxxx #Input folder out_folder = rxxx #Output folde…

How to bind all frame widgets to Enter event

I the following code I want to bind all frame1 items to <Enter> Event, but it does not work. I mean canvas.focus_set() does not take effect. How can I solve my problem?for w in frame1.winfo_chil…

Typeerror takes no arguments [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Unable to access element within page

Form Screenshot HTML Inspect Code screenshotIm trying to access an element within a page. Cannot give out the exact page link owing to security concerns. Im writing a python program that uses selenium …

How to wtite Erlang B and Erlang C formulas in Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic…

Pip is not recognizing the install command (Windows 7, Python 3.3) [duplicate]

This question already has answers here:python3 --version shows "NameError: name python3 is not defined" [duplicate](2 answers)Closed 6 years ago.I am trying to install Python programs using P…

How do I check if 1 is always followed by a 0

In Python, I cannot find a solution as to how to determine whether there is always a 0 following a 1 somewhere in a list of numbers, to form a pair 10. It doesnt have to be direct follower.For clarity,…

Visual Studio Code debugs even when i run without debugging

i just installed VSCode and I used to work on it but now when I try to run without Debugging with Ctrl + F5. it seems to opens up python debug console and debug like below image enter image description…

Mock global function call while importing

Suppose I have a file called a.py with code likeimport mod1 mod1.a()def b():print("hi")Now if I want to mock fun b() then unittest.py while have import statement at top likefrom a import bat …