I have a py file to read data from Wordpress API and pass values to another fields of other API. When values are singles, i have no problem, but i don't know how make that:
When i read one field from the API, the states values, comes with code instead the text value. For example, when the text value in Wordpress is Barcelona, returns B, and i'll need that the value returned will be Barcelona.
One example of code with simple fields values:
oClienteT["Direcciones"] = []
oClienteT["Telefono"] = oClienteW["billing"]["phone"]
oClienteT["NombreFiscal"] = oClienteW["first_name"] " " oClienteW["last_name"]
oClienteT["Direcciones"].append( {
"Codigo" : oClienteW["id"],
"Nombre" : oClienteW["billing"]["first_name"],
"Apellidos" : oClienteW["billing"]["last_name"],
"Direccion" : oClienteW["billing"]["address_1"],
"Direccion2" : oClienteW["billing"]["address_2"],
"Poblacion" : oClienteW["billing"]["state"],
"Provincia" : oClienteW["billing"]["city"]
})
When billing city is Madrid and billing state is madrid, Wordpress returns Madrid and M
I need tell thst when Madrid, returns Madrid, and so on.
Make sure to convert to a JSON object before accessing fields (data = json.loads(json_str))
response = { "billing": { "address_1": "C/GUSTAVO ADOLFO BECQUER, 4", "city": "SEVILLA", "state": "SE"}}
print(response["billing"].get("address_1", None))
I've just got solved it:
def initProvincias(self):
self.aProvincias['C'] = 'A Coruña'
self.aProvincias['VI'] = 'Álava'
def getProvincia(self , sCod ):
if not sCod in self.aProvincias:
_logger.info("PROVINCIA NO ENCONTRADA "+str(sCod))
return ""
return self.aProvincias[sCod]
"Provincia" : self.getProvincia( oClienteW["shipping"]["state"] ),
Related
Hoping you are good
I am trying to get data from zendisk by API and Python Json
i can get any data Value under Audits LikeTicket_id and auther_id but when tried get data under Events such as Body
keep get this error
print(audit['body'])
KeyError: 'body'
JSON Output
{
"audits":[
{
"id":1727876301271,
"ticket_id":54010951,
"created_at":"2021-10-21T10:58:06Z",
"author_id":12306596687,
"metadata":{
"system":{
"client":"GuzzleHttp/6.2.1 curl/7.29.0 PHP/7.1.2",
"ip_address":"x.x.x.x",
"location":"Boardman, OR, United States",
"latitude":45.8491,
"longitude":-119.7143
},
"custom":{
}
},
"events":[
{
"id":1727876301291,
"type":"Comment",
"author_id":366289833251,
"body":"Sehr geehrte Damen und Herren,\n\nIn unserer Bestellung fehlt das Kleid, es war nicht mit dabei, obwohl es hätte drin sein müssen.\nFreundliche Grüße",
"attachments":[
],
"audit_id":1727876301271
},
{
"id":1727876301311,
"type":"Create",
"value":"366289833251",
"field_name":"requester_id"
},
Python Code
import requests
import csv
# Settings
auth = 'xxxxxxx', 'xxxxxx'
view_tickets = []
view_id = 214459268
view_audits = []
ticket_id = 54010951
view_events =[]
print(f'Getting tickets from ticket_id ID {ticket_id}')
url = f'https://xxxx.zendesk.com/api/v2/tickets/54010951/audits.json'
while url:
response = requests.get(url, auth=auth)
page_data = response.json()
audits = page_data['audits'] # extract the "tickets" list from the page
view_audits.extend(audits)
url = page_data['next_page']
for audit in audits:
print(audit['body'])
You know you're overwriting, not adding, to audits right? (in this line: audits = page_data['audits']). I don't think that makes sense, but it's hard to know your intent.
To fix the error itself, your json structure has the body key inside the events key. So you can access it with:
print(audit['events'][0]['body'])
or, using another loop:
for audit in audits:
for event in audit['events']
print(event['body'])
You might get an error for the 2nd one because it doesn't appear to have the body key. You can add an if statement to handle that if you want.
I'm building a small API to interact with our database for other projects. I've built the database and have the API functioning fine, however, the data I get back isn't structured how I want it.
I am using Python with Flask/Flask-Restful for the API.
Here is a snippet of my Python that handles the interaction:
class Address(Resource):
def get(self, store):
print('Received a request at ADDRESS for Store ' + store )
conn = sqlite3.connect('store-db.db')
cur = conn.cursor()
addresses = cur.execute('SELECT * FROM Sites WHERE StoreNumber like ' + store)
for adr in addresses:
return(adr, 200)
If I make a request to the /sites/42 endpoint, where 42 is the site id, this is what I'll receive:
[
"42",
"5000 Robinson Centre Drive",
"",
"Pittsburgh",
"PA",
"15205",
"(412) 787-1330",
"(412) 249-9161",
"",
"Dick's Sporting Goods"
]
Here is how it is structured in the database:
Ultimately I'd like to use the column name as the Key in the JSON that's received, but I need a bit of guidance in the right direction so I'm not Googling ambiguous terms hoping to find something.
Here is an example of what I'd like to receive after making a request to that endpoint:
{
"StoreNumber": "42",
"Street": "5000 Robinson Centre Drive",
"StreetSecondary": "",
"City": "Pittsburgh",
"State": "PA",
"ZipCode": "15205",
"ContactNumber": "(412) 787-1330",
"XO_TN": "(412) 249-9161",
"RelocationStatus": "",
"StoreType": "Dick's Sporting Goods"
}
I'm just looking to get some guidance on if I should change how my data is structured in the database (i.e. I've seen some just put the JSON in their database, but I think that's messy) or if there's a more intuitive method I could use to control my data.
Updated Code using Accepted Answer
class Address(Resource):
def get(self, store):
print('Received a request at ADDRESS for Store ' + store )
conn = sqlite3.connect('store-db.db')
cur = conn.cursor()
addresses = cur.execute('SELECT * FROM Sites WHERE StoreNumber like ' + store)
for r in res:
column_names = ["StoreNumber", "Street", "StreetSecondary","City","State", "ZipCode", "ContactNumber", "XO_TN", "RelocationStatus", "StoreType"]
data = [r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8]]
datadict = {column_names[itemindex]:item for itemindex, item in enumerate(data)}
return(datadict, 200)
You could just convert your list to a dict and then parse it to a JSON string before passing it back out.
// These are the names of the columns in your database
>>> column_names = ["storeid", "address", "etc"]
// This is the data coming from the database.
// All data is passed as you are using SELECT * in your query
>>> data = [42, "1 the street", "blah"]
// This is a quick notation for creating a dict from a list
// enumerate means we get a list index and a list item
// as the columns are in the same order as the data, we can use the list index to pull out the column_name
>>> datadict = {column_names[itemindex]:item for itemindex, item in enumerate(data)}
//This just prints datadict in my terminal
>>> datadict
We now have a named dict containing your data and the column names.
{'etc': 'blah', 'storeid': 42, 'address': '1 the street'}
Now dump the datadict to a string so that it can be sent to the frontend.
>>> import json
>>> json.dumps(datadict)
The dict has now been converted to a string.
'{"etc": "blah", "storeid": 42, "address": "1 the street"}'
This would require no change to your database but the script would need to know about the column names or retrieve them dynamically using some SQL.
If the data in the database is in the correct format for passing to the frontend then you shouldn't need to change the database structure. If it was not in the correct format then you could either change the way it was stored or change your SQL query to manipulate it.
I have this JSON file.
{
"reviewers":[
{
"user":{
"name":"keyname",
"emailAddress":"John#email",
"id":3821,
"displayName":"John Doe",
"active":true,
"slug":"jslug",
"type":"NORMAL",
"link":{
"url":"/users/John",
"rel":"self"
},
},
"role":"REVIEWER",
"approved":true
},
{
"user":{
"name":"keyname2",
"emailAddress":"Harry#email",
"id":6306,
"displayName":"Harry Smith",
"active":true,
"slug":"slug2",
"link":{
"type":"NORMAL",
"url":"/users/Harry",
"rel":"self"
},
},
"role":"REVIEWER",
"approved":false
}
],
}
Initially, I was using a snippet of code that would go through and grab the full names of the reviewers.
def get_reviewers(json):
reviewers = ""
for key in json["reviewers"]:
reviewers += key["user"]["displayName"] + ", "
reviewers = reviewers[:-2]
return reviewers
which would return "John Doe, Harry Smith". However, now I'm trying to get it so that the script will return a (A) next to the name of the user if their tag equals true "approved"=true.
So for example the code above would get the names, then see that John's approved tag is true and Harry's is false, then return "John Doe(A), Harry Smith". I'm just not sure where to even begin to do this. Can anyone point me in the right direction?
This is what I've been trying so far but obviously it isn't working as I'd like it to.
def get_reviewers(stash_json):
reviewers = ""
for key in stash_json["reviewers"]:
if stash_json["reviewers"][0]["approved"] == true:
reviewers += key["user"]["displayName"] + "(A)" + ", "
else:
reviewers += key["user"]["displayName"] + ", "
reviewers = reviewers[:-2]
return reviewers
which outputs Jason Healy(A), Joan Reyes(A)
This is what my stash_json outputs when put through pprint.
You probably want something along the lines of this:
def get_reviewers(stash_json):
reviewers = ""
for item in stash_json["reviewers"]:
if item["approved"]:
reviewers += item["user"]["displayName"] + "(A)" + ", "
else:
reviewers += item["user"]["displayName"] + ", "
reviewers = reviewers[:-2]
return reviewers
I think part of your confusion comes from the fact that "reviewers" is a list of dict elements, and each dict element has a key-value approved, but also a key "user" which value itself is another dict.
Read the JSON file carefully, and for debugging purposes, use plenty of
print(...)
print(type(...)) # whether something is a dict, list, str, bool etc
or
from pprint import pprint # pretty printing
pprint(...)
This looks like a good place to use join and list comprehension:
def get_reviewers(stash_json):
return ", ".join([item['user']['displayName'] + ('(A)' if item['approved'] else '') for item in stash_json['reviewers']])
I'm new to python as was wondering how I could get the estimatedWait and routeName from this string.
{
"lastUpdated": "07:52",
"filterOut": [],
"arrivals": [
{
"routeId": "B16",
"routeName": "B16",
"destination": "Kidbrooke",
"estimatedWait": "due",
"scheduledTime": "06: 53",
"isRealTime": true,
"isCancelled": false
},
{
"routeId":"B13",
"routeName":"B13",
"destination":"New Eltham",
"estimatedWait":"29 min",
"scheduledTime":"07:38",
"isRealTime":true,
"isCancelled":false
}
],
"serviceDisruptions":{
"infoMessages":[],
"importantMessages":[],
"criticalMessages":[]
}
}
And then save this to another string which would be displayed on the lxterminal of the raspberry pi 2. I would like only the 'routeName' of B16 to be saved to the string. How do I do that?
You just have to deserialise the object and then use the index to access the data you want.
To find only the B16 entries you can filter the arrivals list.
import json
obj = json.loads(json_string)
# filter only the b16 objects
b16_objs = filter(lambda a: a['routeName'] == 'B16', obj['arrivals'])
if b16_objs:
# get the first item
b16 = b16_objs[0]
my_estimatedWait = b16['estimatedWait']
print(my_estimatedWait)
You can use string.find() to get the indices of those value identifiers
and extract them.
Example:
def get_vaules(string):
waitIndice = string.find('"estimatedWait":"')
routeIndice = string.find('"routeName":"')
estimatedWait = string[waitIndice:string.find('"', waitIndice)]
routeName = string[routeIndice:string.find('"', routeIndice)]
return estimatedWait, routeName
Or you could just deserialize the json object (highly recommended)
import json
def get_values(string):
jsonData = json.loads(string)
estimatedWait = jsonData['arrivals'][0]['estimatedWait']
routeName = jsonData['arrivals'][0]['routeName']
return estimatedWait, routeName
Parsing values from a JSON file using Python?
Consider the following List of Dictionaries(retrieved from database):
education = [{
"schools" : "Nelson Mandela Metropolitan University",
"studied_from" : new Date("16/1/2012 02:00:00"),
"studied_to" : new Date("25/1/2055 02:00:00"),
"qualifications" : " National Diploma",
"fields_of_study" : "Industrial engineering"
}, {
"schools" : "Massachusetts Institute of Technology",
"studied_from" : new Date("16/1/2009 02:00:00"),
"studied_to" : new Date("25/1/2020 02:00:00"),
"qualifications" : "B Tech",
"fields_of_study" : "Information Technology"
}]
I am trying to get the data from one dict to be returned.
I have tried the following:
for edu in context.education:
studied_to.append(edu.studied_to)
qualification = edu.qualifications
fields_of_study = edu.fields_of_study
school = edu.schools
recent = max(studied_to)
if recent:
print recent
print qualification
print fields_of_study
This is not working as it always returns the data for the data in the last dict,
I also tried putting the if-statement inside the for loop, but this did not work either as then it runs for all entries.
I ultimately want to use these values to generate on an HTML page.
Your code unfortunately makes no sense at all. You seem to be appending some variables to lists, and redefining other variables with the latest version, without ever actually returning the dictionary you want.
It seems however that what you're actually after is the dict with the maximum "studied_to" value. So, you should just replace all that code with max with a key:
most_recent = max(education, key=lambda s: s['studied_to'])
(Also, do please try and post valid syntax for the language you're using. The first snippet appears to be Javascript, not Python.)
I think you are trying to get the data for the most recent education. You should use a variable to hold the most recent date so far and compare that to the entries in your list. See this as a reference:
max_date = None
for edu in education:
# this line is for initialization
if max_date is None:
max_date = edu["studied_to"]
# check if we have a recent entry at hand
if max_date <= edu["studied_to"]:
max_date = edu["studied_to"]
qualification = edu["qualifications"]
fields_of_study = edu["fields_of_study"]
print max_date
print qualification
print fields_of_study
With some correction of syntax:
from operator import itemgetter
from datetime import datetime
education = [{
"schools" : "Nelson Mandela Metropolitan University",
"studied_from" : datetime.strptime("16/1/2012 02:00:00", "%d/%m/%Y %H:%M:%S"),
"studied_to" : datetime.strptime("25/1/2055 02:00:00", "%d/%m/%Y %H:%M:%S"),
"qualifications" : " National Diploma",
"fields_of_study" : "Industrial engineering"
}, {
"schools" : "Massachusetts Institute of Technology",
"studied_from" : datetime.strptime("16/1/2009 02:00:00", "%d/%m/%Y %H:%M:%S"),
"studied_to" : datetime.strptime("25/1/2020 02:00:00", "%d/%m/%Y %H:%M:%S"),
"qualifications" : "B Tech",
"fields_of_study" : "Information Technology"
}]
most_recent = max(education, key=itemgetter('studied_to'))
Thanks to all, your answers were really helpful. I took Daniel's answer and modified it:
edu = sorted(education, key=lambda studied_to: studied_to)
Now I can call edu[0] to get the value of the most recent education:
>>> edu[0]["studied_to"]
datetime.datetime(2055, 1, 25, 2, 0)
>>> edu[-1]["studied_to"]
datetime.datetime(2020, 1, 25, 2, 0)
No matter what ed[0] will always be the latest education.