Invalid boolean value using python requests - python

I am using python requests for posting data in dpd api. If I post the data:
{
"job_id": null,
"collectionOnDelivery": false,
"invoice": null,
"collectionDate": "2012-05-01T09:00:00",
"consolidate": null,
"consignment": []
}
using requests, I am getting the response 200, with error as
{"errorCode":"1008","errorType":"Validation","errorMessage":"Invalid boolean value","obj":"collectionOnDelivery"}
I modified the value of collectionOnDelivery as 0 and tried, but got the same error message. Can anyone help in this?

I guess "null" and "false" aren't a variable or a type you created. In python it is "None" instead of "null", also you need to capitalize first letter of "false" So try this:
{
"job_id": None,
"collectionOnDelivery": False,
"invoice": None,
"collectionDate": "2012-05-01T09:00:00",
"consolidate": None,
"consignment": []
}

Related

Export Json to CSV with missing key in a dict via get()

I am very new to Python, and need to using Python to get the below work done.
I am using Python to get value out for a key pairs (which is the JSON response I got from API call), however, some of them has the value while some of them might not have the value, example of JSON response as below:
"attributes": [
{
"key": "TK_GENIE_ACTUAL_TOTAL_HOURS_EXCLUDE_CORRECTIONS",
"alias": "Annual Leave"
},
{
"key": "TK_GENIE_ACTUAL_TOTAL_HOURS_EXCLUDE_CORRECTIONS",
"alias": "Other Non-Prod Hours"
},
{
"key": "TK_GENIE_ACTUAL_TOTAL_HOURS_EXCLUDE_CORRECTIONS",
"alias": "Non-Prod Hours"
},
{
"key": "EMP_COMMON_PRIMARY_JOB",
"alias": "Primary Job",
"rawValue": "RN",
"value": "RN"
},
{
"key": "TIMECARD_TRANS_APPLY_DATE",
"alias": "Apply Date",
"rawValue": "2022-05-19",
"value": "19/05/2022"
},
The above is one of the children under a nested others, as you can see, for the above one, there is no value for "Annual Leave", however, other children might has a valid value for "Annual Leave"
I am exporting those infor into CSV, with "alias" is the column name, and "value" is the row value
like below csv sample:
enter image description here
So, I using the below python code to extract the value for each key and put them into csv as per column specified.
AL=item['attributes'][0]['value']
Date=item['attributes'][4]['value']
spamwriter.writerow([AL,'2','3',date,'5'])
However, it raised an error code
File "jsoncsv.py", line 47, in <module>
al=item['attributes'][0]['value']
KeyError: 'value'
I think I understand the error, where there is no value for this particular "Annual Leave" key.
But how do I say,like, if there is no value for this key, then value = 0, and put 0 in the CSV under "Annual Leave" column, then, move to next (which is "Other Non-Prod Hours", which also has no value in this case, but might have value for some other children)?
I found get(), but not sure how should I code it, I was trying below code:
value=it.get('value')
if len(value)>0:
AL=item['attributes'][0]value
Date=item['attributes'][4]value
spamwriter.writerow([AL,'2','3',date,'5'])
But result is syntax error.
Could please any Python expert provide help.
Much Appreciated.
WB
As user #richarddodson pointed out, you can do this:
al = item['attributes'][0].get('value', 0)
Instead of 0, you may want to consider using None, which is a clear indication that there is no value, avoiding confusion with the case where value actually is 0, i.e.:
al = item['attributes'][0].get('value', None)
Which is the same as:
al = item['attributes'][0].get('value')
As .get() returns None if there is no value to get.
A more explicit way to do the same would be:
al = None if 'value' in item['attributes'][0] else item['attributes'][0]['value']
But the solution using .get() is simpler and faster and thus probably preferable.

How to parse nested JSON object?

I am working on a new project in HubSpot that returns nested JSON like the sample below. I am trying to access the associated contacts id, but am struggling to reference it correctly (the id I am looking for is the value '201' in the example below). I've put together this script, but this script only returns the entire associations portion of the JSON and I only want the id. How do I reference the id correctly?
Here is the output from the script:
{'contacts': {'paging': None, 'results': [{'id': '201', 'type': 'ticket_to_contact'}]}}
And here is the script I put together:
import hubspot
from pprint import pprint
client = hubspot.Client.create(api_key="API_KEY")
try:
api_response = client.crm.tickets.basic_api.get_page(limit=2, associations=["contacts"], archived=False)
for x in range(2):
pprint(api_response.results[x].associations)
except ApiException as e:
print("Exception when calling basic_api->get_page: %s\n" % e)
Here is what the full JSON looks like ('contacts' property shortened for readability):
{
"results": [
{
"id": "34018123",
"properties": {
"content": "Hi xxxxx,\r\n\r\nCan you clarify on how the blocking of script happens? Is it because of any CSP (or) the script will decide run time for every URL’s getting triggered from browser?\r\n\r\nRegards,\r\nLogan",
"createdate": "2019-07-03T04:20:12.366Z",
"hs_lastmodifieddate": "2020-12-09T01:16:12.974Z",
"hs_object_id": "34018123",
"hs_pipeline": "0",
"hs_pipeline_stage": "4",
"hs_ticket_category": null,
"hs_ticket_priority": null,
"subject": "RE: call followup"
},
"createdAt": "2019-07-03T04:20:12.366Z",
"updatedAt": "2020-12-09T01:16:12.974Z",
"archived": false
},
{
"id": "34018892",
"properties": {
"content": "Hi Guys,\r\n\r\nI see that we were placed back on the staging and then removed again.",
"createdate": "2019-07-03T07:59:10.606Z",
"hs_lastmodifieddate": "2021-12-17T09:04:46.316Z",
"hs_object_id": "34018892",
"hs_pipeline": "0",
"hs_pipeline_stage": "3",
"hs_ticket_category": null,
"hs_ticket_priority": null,
"subject": "Re: Issue due to server"
},
"createdAt": "2019-07-03T07:59:10.606Z",
"updatedAt": "2021-12-17T09:04:46.316Z",
"archived": false,
"associations": {
"contacts": {
"results": [
{
"id": "201",
"type": "ticket_to_contact"
}
]
}
}
}
],
"paging": {
"next": {
"after": "35406270",
"link": "https://api.hubapi.com/crm/v3/objects/tickets?associations=contacts&archived=false&hs_static_app=developer-docs-ui&limit=2&after=35406270&hs_static_app_version=1.3488"
}
}
}
You can do api_response.results[x].associations["contacts"]["results"][0]["id"].
Sorted this out, posting in case anyone else is struggling with the response from the HubSpot v3 Api. The response schema for this call is:
Response schema type: Object
String results[].id
Object results[].properties
String results[].createdAt
String results[].updatedAt
Boolean results[].archived
String results[].archivedAt
Object results[].associations
Object paging
Object paging.next
String paging.next.after
String paging.next.linkResponse schema type: Object
String results[].id
Object results[].properties
String results[].createdAt
String results[].updatedAt
Boolean results[].archived
String results[].archivedAt
Object results[].associations
Object paging
Object paging.next
String paging.next.after
String paging.next.link
So to access the id of the contact associated with the ticket, you need to reference it using this notation:
api_response.results[1].associations["contacts"].results[0].id
notes:
results[x] - reference the result in the index
associations["contacts"] -
associations is a dictionary object, you can access the contacts item
by it's name
associations["contacts"].results is a list - reference
by the index []
id - is a string
In my case type was ModelProperty or CollectionResponseProperty couldn't reach dict anyhow.
For the record this got me to go through the results.
for result in list(api_response.results):
ID = result.id

Parse data from multiple numbers in a list or slice - TypeError: list indices must be integers or slices, not str

The problem
I am trying to get a list of statuses of all support tickets from freshdesk.
So far I have been able to request.get all the tickets but it comes out with too much information. So I am trying to only pull the "status" from the json data.
Here is what happens if I want to print just 1 ticket's information:
ticketresponse = requests.get(freshdeskticketsurl, auth = (freshdeskapi, freshdeskpassword))
ticket_json = ticketresponse.json()
ticket_str = json.dumps(ticket_json[0], indent=2)
print(ticket_str)
Response:
{
"cc_emails": [],
"fwd_emails": [],
"reply_cc_emails": [],
"ticket_cc_emails": [],
"fr_escalated": false,
"spam": false,
"email_config_id": null,
"group_id": 43000110950,
"priority": 1,
"requester_id": taking that out of this example,
"responder_id": taking that out of this example,
"source": 3,
"company_id": taking that out of this example,
"status": 5,
"subject": "Receptiontv-min - can't use slide show in powerpoint",
"association_type": null,
"support_email": null,
"to_emails": null,
"product_id": null,
"id": taking that out of this example,
"type": "Trouble Ticket",
"due_by": "2021-03-09T19:21:09Z",
"fr_due_by": "2022-02-25T19:21:09Z",
"is_escalated": false,
"custom_fields": {},
"created_at": "2021-03-02T19:21:09Z",
"updated_at": "2021-03-02T19:21:16Z",
"associated_tickets_count": null,
"tags": []
}
I ONLY want the "status" section. I can do that with only 1 ticket by doing this:
ticketresponse = requests.get(freshdeskticketsurl, auth = (freshdeskapi, freshdeskpassword))
ticket_json = ticketresponse.json()
ticket_str = json.dumps(ticket_json[0]['status'], indent=2)
print(ticket_str)
Response:
5
However, where I am having trouble is trying to print out the status for all the tickets at once. I can print out all the tickets at once by using ticket_str = json.dumps(ticket_json, indent=2) print(ticket_str)
However if I try any of the following lines of code to try and get all the status, it gives me the same error:
ticket_str = json.dumps(ticket_json['status'], indent=2)
Response
TypeError: list indices must be integers or slices, not str
ticketslice = slice(0,20) ticket_str = json.dumps(ticket_json[ticketslice]['status'], indent=2)
Response
TypeError: list indices must be integers or slices, not str
If anyone has any ideas or has a different approach to the idea let me know.
Thank you for your time,
If you want all the statuses you need to loop on the response array, something like that:
for i in range(len(ticket_json)):
ticket_str = json.dumps(ticket_json[i], indent=2)
print(ticket_str['status'])

getting value of request response object python

I am using python requests to validate mailgun email addresses.
def validateemail(emailaddress):
return requests.get(
"https://api.mailgun.net/v3/address/validate",
auth=("api", EMAILPUBLICVALIDATIONKEY ),
params={'address': emailaddress}
)
validation = validateemail(email)
validationtext = validation.json
validationtext contains the following response:
["{"address": "sdfhdd#assfuck.com", "did_you_mean": …: false, "is_role_address": false, "is_valid": tr", "ue, "mailbox_verification": "unknown", "parts": {"…: "assfuck.com", "local_part": "sdfhdd"}, "reason", "": null}"]
0: "{"address": "sdfhdd#assfuck.com", "did_you_mean": null, "is_disposable_address": false, "is_role_address": false, "is_valid": tr"
1: "ue, "mailbox_verification": "unknown", "parts": {"display_name": null, "domain": "assfuck.com", "local_part": "sdfhdd"}, "reason"
2: "": null}"
in array position 0 there is a is_validproperty. I want to see if its true or not and do some action.
everything I have tried keeps giving me errors
print(validationtext[0].is_valid)
TypeError: 'instancemethod' object has no attribute '__getitem__'
what gives?
validation.json is a function, not an attribute. If you want to get the result, you have to call the function by putting parentheses on the end, as in validationtext = validation.json().
You didn't call the validate.json function correctly. You're missing the parenthesis: validate.json()
Just a minor oversight on your end! Happens to the best of us.

How to submit post data via urllib2 in python 2.4?

I am trying to submit a POST request with data in json format to create a user using the Crowd API.
Here is the code snippet:
url = 'http://crowdserver/crowd/rest/usermanagement/1/user'
payload = '{"name": "sampleuser", "password": {"value": "secret"}, "active": true, "first-name": "Sample", "last-name": "User","display-name": "Sample User", "email": "sample#user.cool"}'
req = urllib2.Request(url, payload)
req.add_header('Content-type','application/json')
req.add_header("Accept", "application/json")
res = urllib2.urlopen(req)
output = resp.read()
print output
print resp.code
I get the following output:
Bad Request
Error code returned is 400
I thought this might perhaps be an encoding issue so replaced payload with:
payload = json.dumps({"name": "sampleuser", "password": {"value": "secret"}, "active": true, "first-name": "Sample", "last-name": "User","display-name": "Sample User", "email": "sample#user.cool"})
Which returns:
NameError: name 'true' is not defined
So it looks like "active": true is not in an acceptable format.
If I add double quotes such as "active":"true" I get a TypeError; TypeError: not a valid non-string sequence or mapping object
Update
So the type error was indeed solved by setting the active attribute value to True but it turns out the 400 error was returned due to invalid user data, for example missing password or the user already exists read as the user already exists - I find it odd that the error for invalid input and an existing user share the same error code.
In this line:
payload = '{"name": "sampleuser", "password": {"value": "secret"}, "active": true, "first-name": "Sample", "last-name": "User","display-name": "Sample User", "email": "sample#user.cool"}'
Change ..., "active": true, ... to ..., "active": True, ...
Python's True/False are case-sensitive.

Categories