How to get specific data from a dictionary in python - python

So I have a json file. Where I load it as json file in python and I try to get the items from the dictionary. I can access the 'sha' the 'author' and get all of its components but how can I access just the 'message', 'name', and 'date' ?
I try this code to access the 'messages' but I get a key error:
data = json.load(json_file)
print(str(type(data))) # class <dict>
for p in data['commits']:
print(p['message'])
My file Looks like this: (It contains more than one commit messages):
{
"commits":[
{
"sha":"exampleSHA",
"node_id":"exampleID",
"commit":{
"author":{
"name":"example",
"email":"example#example.se",
"date":"2015-09-07T22:06:51Z"
},
"committer":{
"name":"example",
"email":"example#example.se",
"date":"2015-09-07T22:06:51Z"
},
"message":"Added LaTex template and instructions",
"tree":{
"sha":"examplesha",
"url":"http://example.url"
},
"parents":[]
}}
]
}
There are many 'messages' that I want to get all of them from the dict.
The result would be this:
AuthorName Message Date
example Added LaTex template and instructions 2013-07-08T20:16:51Z
example2 Message2 2015-09-07T22:06:51Z
.... .... .....

Try this :
data = json.load(json_file)
print(str(type(data))) # class <dict>
for p in data['commits']:
print(p['commit']['message'])
message field is nested inside commit field.

Related

Python: How to look up a specific key in json and add values ​to it?

I need to look for a key in json e add some info that come from a SQl query.
The problem is how to look for this specific key and add the info.
Here's a example:
{
"charts":{
"chart_file": "Z1.xml",
"chart_path":"...path_to_chart",
"modified":"date",
"chart_tags":{}
}
}
The query give me info about the "chart_tags" and initially its empty.
Assuming there are 3 different tags, I would like to add the information being the tag name the main key and inside it the information of colors, scales and ID.
Here's a desired output:
{
"charts":{
"chart_file": "Z1.xml",
"chart_path":"...path_to_chart",
"modified":"date",
"chart_tags":{
"tagNAME":{
"tagID":1,
"scale":"[0,100]",
"color":"#000000"
},
"tagNAME2":{
"tagID":2,
"scale":"[0,5]",
"color":"#e3dc19"
},
"tagNAME3":{
"tagID":3,
"scale":"[20,80]",
"color":"#23fa67"
}
}
}
}
I've been trying to add the information in a dict and right after a json.dump, but I've been having problems with the dict not being hashable.
Here's what i tryed and thanks for the help
jsonFile = open('teste.config', 'r', encoding='utf-8-sig')
millexcsMills = json.load(jsonFile)
currentMill=millexcsMills["mills"][millName]
for x, y,z,w in zip(df1.columns.tolist()[1:], chartTrendColors,chartTrendScales,chartTagID):
data = {
"tagName":x,
"color":y,
"scale": z,
"tagID":w,
}
millexcsMills['mills'][currentMill]['charts']['chart_tags'].append(data)
with open('teste.config', 'w') as outfile:
json.dump(jsonFile, outfile, indent=6)

How to get the particular values from response using python

I am trying to get the values from JSON which is nested.
response = {
"Instance":[
{
"id":"id-1",
"Tags":[],
},
{
"id":"id-2",
"Tags":[],
},
{
"id":"id-3",
"Tags":[
{
"Key":"test",
"Value":"test"
}
],
}
]
}
and the python code i tried is
if response["Instance"]:
print("1-->",response["Instance"])
for identifier in response["Instance"]:
print("2-->", identifier)
if identifier["id"]:
print("3-->", identifier["id"])
if identifier["Tags"]: #ERROR Thrown here as 'Tags' in exception
print("4-->", identifier["Tags"])
for tag in identifier["Tags"]:
print("5-->", identifier["Tags"])
if tag['Key'] == 'test' and tag['Value'] == 'test':
print("6--> test present ")
I am trying to parse through all ids and get the tags that contain the test key. But getting error in 3rd loop when tags has some values. Error in exception is just telling 'Tags'
How can I modify the code?
You are testing for the presence of the key "Tags" in the identifier dict as follows:
if identifier["Tags"]:
# iterate over identifier["Tags"]
If "Tags" is not present in the identifier dict, this will result in:
KeyError: 'Tags'
Instead, you should use:
if "Tags" in identifier:
# iterate over identifier["Tags"]

How to extract specific data from JSON object using python?

I'm trying to scrape a website and get items list from it using python. I parsed the html using BeaufitulSoup and made a JSON file using json.loads(data). The JSON object looks like this:
{ ".1768j8gv7e8__0":{
"context":{
//some info
},
"pathname":"abc",
"showPhoneLoginDialog":false,
"showLoginDialog":false,
"showForgotPasswordDialog":false,
"isMobileMenuExpanded":false,
"showFbLoginEmailDialog":false,
"showRequestProductDialog":false,
"isContinueWithSite":true,
"hideCoreHeader":false,
"hideVerticalMenu":false,
"sequenceSeed":"web-157215950176521",
"theme":"default",
"offerCount":null
},
".1768j8gv7e8.6.2.0.0__6":{
"categories":[
],
"products":{
"count":12,
"items":[
{
//item info
},
{
//item info
},
{
//item info
}
],
"pageSize":50,
"nextSkip":100,
"hasMore":false
},
"featuredProductsForCategory":{
},
"currentCategory":null,
"currentManufacturer":null,
"type":"Search",
"showProductDetail":false,
"updating":false,
"notFound":false
}
}
I need the items list from product section. How can I extract that?
Just do:
products = jsonObject[list(jsonObject.keys())[1]]["products"]["items"]
import json packagee and map every entry to a list of items if it has any:
This solution is more universal, it will check all items in your json and find all the items without hardcoding the index of an element
import json
data = '{"p1": { "pathname":"abc" }, "p2": { "pathname":"abcd", "products": { "items" : [1,2,3]} }}'
# use json package to convert json string to dictionary
jsonData = json.loads(data)
type(jsonData) # dictionary
# use "list comprehension" to iterate over all the items in json file
# itemData['products']["items"] - select items from data
# if "products" in itemData.keys() - check if given item has products
[itemData['products']["items"] for itemId, itemData in jsonData.items() if "products" in itemData.keys()]
Edit: added comments to code
I'll just call the URL of the JSON file you got from BeautifulSoup "response" and then put in a sample key in the items array, like itemId:
import json
json_obj = json.load(response)
array = []
for i in json_obj['items']:
array[i] = i['itemId']
print(array)

Python GraphQL API call composition

I've recently started learning how to use python and i'm having some trouble with a graphQL api call.
I'm trying to set up a loop to grab all the information using pagination, and my first request is working just fine.
values = """
{"query" : "{organizations(ids:) {pipes {id name phases {id name cards_count cards(first:30){pageInfo{endCursor hasNextPage} edges {node {id title current_phase{name} assignees {name} due_date createdAt finished_at fields{name value filled_at updated_at} } } } } }}}"}
"""
but the second call using the end cursor as a variable isn't working for me. I assume that it's because i'm not understanding how to properly escape the string of the variable. But for the life of me I'm unable to understand how it should be done.
Here's what I've got for it so far...
values = """
{"query" : "{phase(id: """ + phaseID+ """ ){id name cards_count cards(first:30, after:"""\" + pointer + "\"""){pageInfo{endCursor hasNextPage} edges {node {id title assignees {name} due_date createdAt finished_at fields{name value datetime_value updated_at phase_field { id label } } } } } } }"}
"""
the second one as it loops just returns a 400 bad request.
Any help would be greatly appreciated.
As a general rule you should avoid building up queries using string manipulation like this.
In the GraphQL query itself, GraphQL allows variables that can be placeholders in the query for values you will plug in later. You need to declare the variables at the top of the query, and then can reference them anywhere inside the query. The query itself, without the JSON wrapper, would look something like
query = """
query MoreCards($phase: ID!, $cursor: String) {
phase(id: $phase) {
id, name, cards_count
cards(first: 30, after: $cursor) {
... CardConnectionData
}
}
}
"""
To actually supply the variable values, they get passed as an ordinary dictionary
variables = {
"phase": phaseID,
"cursor": pointer
}
The actual request body is a straightforward JSON structure. You can construct this as a dictionary too:
body = {
"query": query,
"variables": variables
}
Now you can use the standard json module to format it to a string
print(json.dumps(body))
or pass it along to something like the requests package that can directly accept the object and encode it for you.
I had a similar situation where I had to aggregate data through paginating from a GraphQL endpoint. Trying the above solution didn't work for me that well.
to start my header config for graphql was like this:
headers = {
"Authorization":f"Bearer {token}",
"Content-Type":"application/graphql"
}
for my query string, I used the triple quote with a variable placeholder:
user_query =
"""
{
user(
limit:100,
page:$page,
sort:[{field:"email",order:"ASC"}]
){
list{
email,
count
}
}
"""
Basically, I had my loop here for the pages:
for page in range(1, 9):
formatted_query = user_query.replace("$page",f'{page}')
response = requests.post(api_url, data=formatted_query,
headers=headers)
status_code, json = response.status_code, response.json()

Using JSON dict in Django

I'm trying to access certain fields of information in JSON dict. My code is set up as the following:
Views.py
def viewIssues(request):
r = requests.get(bucket_url)
issue_payload = r.json()
issue = json.loads(str(issue_payload))
context = {
"issue_title": issue['issues']['title'],
"issue_content": issue['issues']['content'],
"title": "View Issues",
}
return render(request, "view_issues.html", context)
str(issue_payload) gives me this:
{
'search':None,
'count':1,
'filter':{
},
'issues':[
{
'priority':'major',
'comment_count':0,
'utc_created_on':'2016-11-12 01:48:16+00:00',
'utc_last_updated':'2016-11-12 01:48:16+00:00',
'status':'new',
'title':'example issue',
'reported_by':{
'is_staff':False,
'display_name':'display name',
'is_team':False,
'resource_uri':'/1.0/users/username',
'avatar':'https://bitbucket.org/account/username/avatar/32/?ts=1479493904',
'first_name':'firstname',
'username':'username',
'last_name':'lastname'
},
'is_spam':False,
'content':'blah blah',
'metadata':{
'milestone':None,
'component':None,
'version':None,
'kind':'bug'
},
'local_id':1,
'created_on':'2016-11-12T02:48:16.052',
'resource_uri':'/1.0/repositories/username/supportal2016test/issues/1',
'follower_count':1
}
]
}
However when I try to use the json.loads and indices ['issues']['title'] and ['issues']['title'] I get an error:
JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
I'm wondering if it's because the converted payload has quotations on each field (i.e. 'issues'). Any help would be much appreciated.
The .json() call already parses the JSON result and returns a Python structure in this case a dictionary. Then your call
issue = json.loads(str(issue_payload))
forces the dictionary into a string and tries to parse it again. But the dictionary string representation contains ' around strings and not " as required in JSON.
To cut the long story short: issue_payload is what you want already.

Categories