Reputation: 37
I have the following dictionary data.
"tags": {
"tags":
[
{
"applied_at": 1645181955,
"applied_by":
{
"id": "1288541",
"type": "admin"
},
"id": "6355625",
"name": "No Reply > 2hrs",
"type": "tag"
},
{
"applied_at": 1645185249,
"applied_by":
{
"id": "4597637",
"type": "admin"
},
"id": "6258671",
"name": "Resolution Provided",
"type": "tag"
},
{
"applied_at": 1647238491,
"applied_by":
{
"id": "4597637",
"type": "admin"
},
"id": "6258671",
"name": "Resolution Provided",
"type": "tag"
}
],
"type": "tag.list"
}
As you see from the above, we have a tag called Resolution Provided
.
My condition is to get the first resolution applied time and second resolution applied time and it may go on and on like n times. The time you can get from the same group field named as applied_at
.
I am trying to achieve this via Python itself.
Can someone help me with an approach, I am not developer but trying to be one. So any constructive critics are fine with me. All I need is a logic so that I can try it.
Upvotes: 0
Views: 222
Reputation: 54
The first that i can come up with is first reach the "tags" and I after that I'll see a list of dicts. So I can use a for loop in here to traverse on those dicts. And then I'll reach the keys easily and I'll do what I want to with those values.
json_data["tags"]["tags"]
Takes you to the list of dictionaries that you wanna iterate it with a for loop.
for current_dict in json_data["tags"]["tags"]:
if current_dict["name"] == "Resolution Provided":
print(current_dict["applied_at"])
Upvotes: -1
Reputation: 23129
I don't understand exactly what you want to do. What I get from your description is that tags with the name "Resolution Provided" are important to you, and for those tags, you want to do something with the "applied_at" field.
Note that the "JSON" you provide is actually not valid JSON. You need an extra set of curly braces around that data to make it valid JSON.
Here's how to extract the "applied_at" fields for tags that have the name "Resolution Provided". This code adds the extra curly braces to allow your data to be parsed as valid JSON:
import json
data = json.loads("{" + data + "}")
for tag in data['tags']['tags']:
if tag['name'] == "Resolution Provided":
print(tag['applied_at'])
Result:
1645185249
1647238491
Upvotes: 1
Reputation: 2505
As I said in comment, first you want to parse the json string into dictionary
import json
a = """
{
"tags": {
"tags":
[
{
"applied_at": 1645181955,
"applied_by":
{
"id": "1288541",
"type": "admin"
},
"id": "6355625",
"name": "No Reply > 2hrs",
"type": "tag"
},
{
"applied_at": 1645185249,
"applied_by":
{
"id": "4597637",
"type": "admin"
},
"id": "6258671",
"name": "Resolution Provided",
"type": "tag"
},
{
"applied_at": 1647238491,
"applied_by":
{
"id": "4597637",
"type": "admin"
},
"id": "6258671",
"name": "Resolution Provided",
"type": "tag"
}
],
"type": "tag.list"
}
}
"""
parsed = json.loads(a)
print(parsed)
Outputs:
{'tags': {'tags': [{'applied_at': 1645181955, 'applied_by': {'id': '1288541', 'type': 'admin'}, 'id': '6355625', 'name': 'No Reply > 2hrs', 'type': 'tag'}, {'applied_at': 1645185249, 'applied_by': {'id': '4597637', 'type': 'admin'}, 'id': '6258671', 'name': 'Resolution Provided', 'type': 'tag'}, {'applied_at': 1647238491, 'applied_by': {'id': '4597637', 'type': 'admin'}, 'id': '6258671', 'name': 'Resolution Provided', 'type': 'tag'}], 'type': 'tag.list'}}
Then you want to sort by it's applied_at
tag:
tags = parsed["tags"]["tags"]
tags.sort(key=lambda i: i["applied_at"])
print(tags)
outputs:
[{'applied_at': 1645181955, 'applied_by': {'id': '1288541', 'type': 'admin'}, 'id': '6355625', 'name': 'No Reply > 2hrs', 'type': 'tag'}, {'applied_at': 1645185249, 'applied_by': {'id': '4597637', 'type': 'admin'}, 'id': '6258671', 'name': 'Resolution Provided', 'type': 'tag'}, {'applied_at': 1647238491, 'applied_by': {'id': '4597637', 'type': 'admin'}, 'id': '6258671', 'name': 'Resolution Provided', 'type': 'tag'}]
The two outputs is identical because your initial json file is already sorted.
Then if you want to know which tag is first applied, just call tags[0]
, second one by tags[1]
, and so on
output of print(tags[0])
{'applied_at': 1645181955, 'applied_by': {'id': '1288541', 'type': 'admin'}, 'id': '6355625', 'name': 'No Reply > 2hrs', 'type': 'tag'}
Upvotes: 1