I'm working on taking a JSON feed and filtering out only the items I want from my list. I'm appending the items I'd like to keep to each list identifier. However, when I convert to JSON the output is incorrect. You can see the ACTUAL OUTPUT example below. The target output below is what I'm actually expecting. I've tried orienting the list with index and records, but no luck.
#TARGET OUTPUT
{
"id":"1",
"Name":"xxx",
"Image":"https://xxx.xxx.png",
},
{
"id":"2",
"Name":"xx2",
"Image":"https://xx2.xxx.png",
}
#ACTUAL OUTPUT
{
"id": ["1","2",]
},
{
"image":["https://xxx.xxx.png","https://xx2.xxx.png"]
},
{
"name":["xxx", "xx2"]
},
#CODE
# JSON feed
{
"document": {
"id": "1",
"image": "https://xxx.xxx.png",
"name": "xxx",
},
},
{
"document": {
"id": "2",
"image": "https://xx2.xxx.png",
"name": "xx2",
},
},
# create list array
list = {'id':[], 'Name': [], 'Image': []}
links = {'id': [], 'Image': []}
# loop through and append items
def getData(hits):
for item in filter(None, hits):
item = item['document']
list['id'].append(item['id'])
links['id'].append(item['id'])
links['Image'].append(item['image'])
list['Image'].append(item['image'])
list['Name'].append(item['name'])
# get first page
pageNum = 1
data = getDataPerPage(pageNum)
try:
itemsNo = data['found']
getData(data['hits'])
while itemsNo > 24:
itemsNo -= 24
pageNum += 1
data = getDataPerPage(pageNum)
getData(data['hits'])
except:
print("broken")
# save list to json
with open('./output/data_chart.json', 'w') as f:
f.write(json.dumps(list))
When you receive multiple JSON objects, those are in the form of a list (so between []). You could:
covert JSON string to python dictionary using json.loads()
filter using the dict
dump dictionary into a JSON string using json.dumps()
input = """[
{"document":
{"id": "1","image": "https://xxx.xxx.png","name": "xxx"}},
{"document":
{"id": "2","image": "https://xx2.xxx.png","name": "xx2"}}
]"""
input_dic = json.loads(input)
tmp = []
for item in input_dic:
tmp.append(json.dumps(item["document"]))
output = json.dumps(tmp)
print(output)
Hope I got your question.
It's not 100% clear what you have or what you want, but with a few assumptions (input is list of dict, desired output is list of dict):
json_obj = [
{
"document": {
"id": "1",
"image": "https://xxx.xxx.png",
"name": "xxx",
},
},
{
"document": {
"id": "2",
"image": "https://xx2.xxx.png",
"name": "xx2",
},
},
]
desired_output = [x["document"] for x in json_obj]
print(desired_output)
Related
Trying to append to a nested json file
My goal is to append some values to a JSON file.
Here is my original JSON file
{
"web": {
"all": {
"side": {
"tags": [
"admin"
],
"summary": "Generates",
"operationId": "Key",
"consumes": [],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "YES",
"schema": {
"type": "string"
}
}
},
"Honor": [
{
"presidential": []
}
]
}
}
}
}
It is my intention to add two additional lines inside the key "Honor", with the values "Required" : "YES" and "Prepay" : "NO". As a result of appending the two values, I will have the following JSON file.
{
"web": {
"all": {
"side": {
"tags": [
"admin"
],
"summary": "Generates",
"operationId": "Key",
"consumes": [],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "YES",
"schema": {
"type": "string"
}
}
},
"Honor": [
{
"presidential": [],
"Required" : "YES",
"Prepay" : "NO"
}
]
}
}
}
}
Below is the Python code that I have written
import json
def write_json(data,filename ="exmpleData.json"):
with open(filename,"w") as f:
json.dump(data,f,indent=2)
with open ("exmpleData.json") as json_files:
data= json.load(json_files)
temp = data["Honor"]
y = {"required": "YES","type": "String"}
temp.append(y)
write_json(data)
I am receiving the following error message:
** temp = data["Honor"] KeyError: 'Honor'
**
I would appreciate any guidance that you can provide to help me achieve my goal. I am running Python 3.7
'Honor' is deeply nested in other dictionaries, and its value is a 1-element list containing a dictionary. Here's how to access:
import json
def write_json(data, filename='exmpleData.json'):
with open(filename, 'w') as f:
json.dump(data, f, indent=2)
with open('exmpleData.json') as json_files:
data = json.load(json_files)
# 'Honor' is deeply nested in other dictionaries
honor = data['web']['all']['side']['Honor']
# Its value is a 1-element list containing another dictionary.
honor[0]['Required'] = 'YES'
honor[0]['Prepay'] = 'NO'
write_json(data)
I'd recommend that you practice your fundamentals a bit more since you're making many mistakes in your data structure handling. The good news is, your JSON load/dump is fine.
The cause of your error message is that data doesn't have an "Honor" property. Data only has a "web" property, which contains "all" which contains "side" which contains "Honor", which contains an array with a dictionary that holds the properties you are trying to add to. So you want to set temp with temp = data['web']['all']['side']['Honor'][0]
You also cannot use append on python dictionaries. Instead, check out dict.update().
I'm looking to grab the displayValue from objectAttributeValues where the objectTypeAttributeId = 14
there are multiple arrays like this, and the position of objectTypeAttributeId = 14 isn't always the same. how do I loop over every array to get that specific displayValue?
I've got something that looks through every possible array, but I want to clean it up.
sample json:
{
"objectEntries": [{
"attributes": [{
"id": "5210",
"objectAttributeValues": [{
"displayValue": "10/Nov/22 3:33 PM",
"referencedType": false,
"searchValue": "2022-11-10T15:33:49.298Z",
"value": "2022-11-10T15:33:49.298Z"
}],
"objectId": "1201",
"objectTypeAttributeId": "12"
},
{
"id": "5213",
"objectAttributeValues": [{
"displayValue": "02f9ed75-b416-49d0-8515-0601581158e5",
"referencedType": false,
"searchValue": "02f9ed75-b416-49d0-8515-0601581158e5",
"value": "02f9ed75-b416-49d0-8515-0601581158e5"
}],
"objectId": "1201",
"objectTypeAttributeId": "14"
},
{
"id": "5212",
"objectAttributeValues": [{
"displayValue": "",
"referencedType": false,
"searchValue": "",
"value": ""
}],
"objectId": "1201",
"objectTypeAttributeId": "11"
}
]
},
{
"attributes": [{
"id": "4263",
"objectAttributeValues": [{
"displayValue": "427904c5-e2c8-4735-bc38-4013928cd043",
"referencedType": false,
"searchValue": "427904c5-e2c8-4735-bc38-4013928cd043",
"value": "427904c5-e2c8-4735-bc38-4013928cd043"
}],
"objectId": "1011",
"objectTypeAttributeId": "14"
},
{
"id": "4262",
"objectAttributeValues": [{
"displayValue": "",
"referencedType": false,
"searchValue": "",
"value": ""
}],
"objectId": "1011",
"objectTypeAttributeId": "11"
}
]
}
]
}
for this sample query, the values would be:
02f9ed75-b416-49d0-8515-0601581158e5
427904c5-e2c8-4735-bc38-4013928cd043
this is my code so far, and would like to make it for efficient:
from jira import JIRA
import requests
import json
base_url = "url"
auth = basic_auth=('user', 'pass')
headers = {
"Accept": "application/json"
}
pages = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for page in pages:
response = requests.request("GET",base_url + '?page=' + str(page),headers=headers,auth=auth)
all_output = json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": "))
output_dict = json.loads(response.text)
output_list = output_dict["objectEntries"]
for outputs in output_list:
print(outputs["attributes"][0]["objectId"])
print(outputs["name"])
print(outputs["objectKey"])
if len(outputs["attributes"][0]["objectAttributeValues"][0]["displayValue"])==36:
print(outputs["attributes"][0]["objectAttributeValues"][0]["displayValue"])
if len(outputs["attributes"][1]["objectAttributeValues"][0]["displayValue"])==36:
print(outputs["attributes"][1]["objectAttributeValues"][0]["displayValue"])
if len(outputs["attributes"][2]["objectAttributeValues"][0]["displayValue"])==36:
print(outputs["attributes"][2]["objectAttributeValues"][0]["displayValue"])
if len(outputs["attributes"][3]["objectAttributeValues"][0]["displayValue"])==36:
print(outputs["attributes"][3]["objectAttributeValues"][0]["displayValue"])
if len(outputs["attributes"][4]["objectAttributeValues"][0]["displayValue"])==36:
print(outputs["attributes"][4]["objectAttributeValues"][0]["displayValue"])
print('\n')
Any suggestions would be appreciated!!
If structure is not changing then this can the solution It will iterate over all objects and add displayValue in search_values list
display_values = []
for object_entries in output_dict.get("objectEntries", []):
for attribute in object_entries.get("attributes"):
if attribute.get("objectTypeAttributeId") == "14":
for object_attr in attribute.get("objectAttributeValues", []):
if object_attr.get("displayValue") not in display_values:
display_values.append(object_attr.get("displayValue"))
print(display_values)
You could browse your JSON dict and proceed each entries until you get the one(s) you are interested in.
# lets browse top level entries of your array
for e1 in outputs["objectEntries"]:
# for each of those entries, browse the entries in the attribute section
for e2 in e1["attributes"]:
# does the entry match the rule "14"? If not, go to the next one
if (e2["objectTypeAttributeId"] != 14):
continue
# print the current entry's associated value
for attr in e2["objectAttributeValues"]
print(attr["displayValue"])
You can iterate over your dict and check if the values matches with a function like this:
def get_display_value(my_dict, value):
results = []
for objectEntries in my_dict['objectEntries']:
for attributes in objectEntries['attributes']:
if int(attributes['objectTypeAttributeId']) == value:
results.append(attributes['objectAttributeValues'][0]['displayValue'])
return results
Using the function:
results = get_display_value(my_dict, 14)
print(results)
Outputs:
['02f9ed75-b416-49d0-8515-0601581158e5', '427904c5-e2c8-4735-bc38-4013928cd043']
Edit: now returning all match values instead of only the first one.
I requested a JSON File from API. Then I want to parse/save just some values out of the File.
Code:
First the request call:
request = requests.get(self.url, verify = False)
request_data = request.json()
Example File (Type <class 'list'>):
[
{
"id1": null,
"id2": "test1",
"id3": "test2",
}
{
"id1": null,
"id2": "test3",
"id3": "test4",
}
]
Now I just want to get all id2 and their value, I tried with the code below, but it only works with dictionaries, how can I convert the list to a dictionary? Or can I extract the values with a list too?
value = {}
output_data = {}
value["id2"] = data.get("id2")
output_data.append(value)
It looks like you have a list of dictionaries. One Pythonic way of dealing with this would be:
all_id2s = [entry['id2'] for entry in request_data]
This would create an array containing just the "id2" elements, like:
['test1', 'test3']
Check it out on IDEOne: https://ideone.com/5iVZhw
You can do as below:
var = [
{
"id1": "null",
"id2": "test1",
"id3": "test2",
},
{
"id1": "",
"id2": "test3",
"id3": "test4",
}
]
output = []
for i in var:
output.append(i['id2'])
print(output)
I have a list of json objects (dictionaries) ds_list
ds_list = [ { "status": "NEW" }, { "status": "UP_TO_DATE" }]
I need to modify an attribute of each object.
So here is my solution:
if we_are_processing:
result = list(map(lambda ds: ds.update({'status': 'PROCESSING'}) or ds, ds_list))
result = [ { "status": "PROCESSING" }, { "status": "PROCESSING" }]
It works, but I don't like it very much, in particular update() and or ds.
What is more pythonic (readable) way of implementing it?
The Pythonic way is to use for loop:
ds_list = [ { "status": "NEW" }, { "status": "UP_TO_DATE" }]
for item in ds_list:
item['status'] = 'PENDING'
# [{'status': 'PENDING'}, {'status': 'PENDING'}]
I have two dictionaries in python 3, called a and b which when dumped as JSON come out as below:
a = {"person": 26.94, "car": 99.49, "dog": 50.56}
b = {"filename": "1234.jpg", "model": "model1", "prototxt": "prototxt.txt"}
I need to combine these into the JSON format below but I am a bit lost on the approach in python, so welcome any pointers!
{
"payload": {
"config": [{
"model": "model1",
"filename": "1234.jpg",
"prototxt": "prototxt.txt"
}],
"results": [{
"object": "person",
"value": 26.94
},
{
"object": "car",
"value": 99.49
},
{
"object": "dog",
"value": 50.56
}
]
}
}
You can achieve this by the following code:
a = {"person": 26.94, "car": 99.49, "dog": 50.56}
b = {"filename": "1234.jpg", "model": "model1", "prototxt": "prototxt.txt"}
a_list= []
for record in a:
a_list.append({'object':record, 'value':a[record]})
payload = {'config':[b], 'results':a_list}
data = {"payload":payload}
# You can print this in the terminal/notebook with
print json.dumps(data, indent=4)
# Or save with
json_string = json.dumps(data)
with open('/path/to/file/payload.json', 'w') as outfile:
json.dump(data, outfile)
It's a matter of making entries in dictionaries and packaging them up in strings to add into new higher dictionaries.