finding a specific object within duplicate element names, python with json - python

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.

Related

convert list array to json

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)

Find a value in a list of dictionaries

I have the following list:
{
"id":1,
"name":"John",
"status":2,
"custom_attributes":[
{
"attribute_code":"address",
"value":"st"
},
{
"attribute_code":"city",
"value":"st"
},
{
"attribute_code":"job",
"value":"test"
}]
}
I need to get the value from the attribute_code that is equal city
I've tried this code:
if list["custom_attributes"]["attribute_code"] == "city" in list:
var = list["value"]
But this gives me the following error:
TypeError: list indices must be integers or slices, not str
What i'm doing wrong here? I've read this solution and this solution but din't understood how to access each value.
Another solution, using next():
dct = {
"id": 1,
"name": "John",
"status": 2,
"custom_attributes": [
{"attribute_code": "address", "value": "st"},
{"attribute_code": "city", "value": "st"},
{"attribute_code": "job", "value": "test"},
],
}
val = next(d["value"] for d in dct["custom_attributes"] if d["attribute_code"] == "city")
print(val)
Prints:
st
Your data is a dict not a list.
You need to scan the attributes according the criteria you mentioned.
See below:
data = {
"id": 1,
"name": "John",
"status": 2,
"custom_attributes": [
{
"attribute_code": "address",
"value": "st"
},
{
"attribute_code": "city",
"value": "st"
},
{
"attribute_code": "job",
"value": "test"
}]
}
for attr in data['custom_attributes']:
if attr['attribute_code'] == 'city':
print(attr['value'])
break
output
st

How to modify a string inside a JSON object and convert it into another JSON object and thus add a key value with Python?

I would like to find a way to convert a string inside my JSON object into another JSON object (nested) with the use of Python and thus add a key to the values inside my string that are separated by the use of a ";".
My current JSON object:
{
"reference": "#############",
"messageContext": "",
"from": {
"number": "+#############",
"name": ""
},
"to": {
"number": "#############"
},
"message": {
"text": "12302;8;6245;d517666e-41ca-459a-9b35-c49386df537b;2;2;50.8447;-4.3614;2021-04-28T22:24:12.204Z;rec123;PRD",
"media": {
"mediaUri": "",
"contentType": "",
"title": ""
},
"custom": {}
},
"groupings": [
"",
"",
""
],
"time": "2021-05-02 14:03:22",
"timeUtc": "2021-05-02T12:03:22",
"channel": "Sms"
}
The format I would try to have is something like this, where I would change the text to an object while adding key values.
Result, which I try to obtain (Inside of text:" "):
{
"reference": ""#############",",
"messageContext": "",
"from": {
"number": "+"#############",",
"name": ""
},
"to": {
"number": ""#############","
},
"message": {
"text": {
"APSI": "12302",
"idVhl": 8,
"idDrv": 6245,
"GUID": "d517666e-41ca-459a-9b35-c49386df537b",
"idLne": 2,
"idSvc": 2,
"Lat": 50.8447,
"Lon": -4.3614,
"Timestamp": "2021-04-28T22:24:12.204Z",
"Rec": "rec123",
"Env": "PRD"
},
"media": {
"mediaUri": "",
"contentType": "",
"title": ""
},
"custom": {}
},
"groupings": [
"",
"",
""
],
"time": "2021-05-02 14:03:22",
"timeUtc": "2021-05-02T12:03:22",
"channel": "Sms"
}
Given your JSON object as input this function will return your expected output.
def transform(input):
text = input['message']['text']
text_list = text.split(';')
text_dict = {
'APSI': text_list[0],
'idVhl': int(text_list[1]),
'idDrv': int(text_list[2]),
'GUID': text_list[3],
'idLne': int(text_list[4]),
'idSvc': int(text_list[5]),
'Lat': float(text_list[6]),
'Lon': float(text_list[7]),
'Timestamp': text_list[8],
'Rec': text_list[9],
'Env': text_list[10]
}
input['message']['text'] = text_dict
return input
This is done by splitting your text on the ; character which returns a list of all the values you want. You can then parse the list into a dictionary and then assign that dictionary to your original text value.

json parsing in python with condition

Could you give me some advice about parsing list from list of dictionary with some conditions in Python3.
From below list of json, I want to extract the max timestamp for each "id".
So the final output I want to have is like
[{
"id": 1,
"value": {
"val1": 400000000,
"val2": 750000000
},
"timestamp": 1600957822.2510917
},
{
"id": 2,
"value": {
"val1": 400000000,
"val2": 750000000
},
"timestamp": 1600958083.618805
}]
Is there any way I can do? Thanks in advance
[
{
"id": 1,
"value": {
"val1": 400000000,
"val2": 750000000
},
"timestamp": 1600957822.2510917
},
{
"id": 2,
"value": {
"val1": 400000000,
"val2": 750000000
},
"timestamp": 1600957857.3018847
},
{
"id": 2,
"value": {
"val1": 400000000,
"val2": 750000000
},
"timestamp": 1600958027.4114041
},
{
"id": 2,
"value": {
"val1": 400000000,
"val2": 750000000
},
"timestamp": 1600958083.618805
}]
If it's list of dictionaries (as spotted in the comments) here is a way to parse it and retrieve a final list of dict:
result = {}
for obj in dict_list:
if obj['id'] in result:
if result[obj['id']]['timestamp'] < obj['timestamp']:
result[obj['id']]['timestamp'] = obj['timestamp']
else:
result[obj['id']] = obj
[x for x in result.values()]
Here is an example of parsing:
import json
# load json
data = json.loads(source)
result = {}
# iterate over json objects
for d in data:
# check whether id is already in result map or not
if d['id'] in result:
# if id already in result, compare timestamps
if d['timestamp'] > result[d['id']]:
result[d['id']] = d['timestamp']
else:
# if id not in result, add id / timestamp pair
result[d['id']] = d['timestamp']
print (result)

How to Fetch the value of any item from the JSON output in Python?

I have a function in python which is fetching the data in JSON format and I need to get the value of one item and store it in variable so I could use it another function
import requests
import json
import sys
def printResponse(r):
print '{} {}\n'.format(json.dumps(r.json(),
sort_keys=True,
indent=4,
separators=(',', ': ')), r)
r = requests.get('https://wiki.tourist.com/rest/api/content',
params={'title' : 'Release Notes for 1.1n1'},
auth=('ABCDE', '*******'))
printResponse(r)
getPageid = json.loads(r)
value = int(getPageid['results']['id'])
I am trying to get the value of id(160925) item in variable "value" so I could use it another function
Below is the JSON OUTPUT
{
"_links": {
"base": "https://wiki.tourist.com",
"context": "",
"self": "https://wiki.tourist.com/rest/api/content?title=Notes+for+1.1u1"
},
"limit": 25,
"results": [
{
"_expandable": {
"ancestors": "",
"body": "",
"children": "/rest/api/content/160925/child",
"container": "",
"descendants": "/rest/api/content/160925/descendant",
"history": "/rest/api/content/160925/history",
"metadata": "",
"operations": "",
"space": "/rest/api/space/Next",
"version": ""
},
"_links": {
"self": "https://wiki.tourist.com/rest/api/content/160925412",
"tinyui": "/x/5IaXCQ",
"webui": "/display/Next/Notes+for+1.1u1"
},
"extensions": {
"position": "none"
},
"id": "160925",
"status": "current",
"title": "Notes for 1.1u1",
"type": "page"
}
],
"size": 1,
"start": 0
} <Response [200]>
It looks like the "results" key in the JSON response corresponds to a list, so you'll want to index into that list to get a dict.
E.g. getPageid['results'][0]['id'] should return you the string value of the "id" key for the first object in the "results" list

Categories