How to remove double quote from key value in dictionary - python

I have this payload:
"cardType": "CHOICE",
"step": 40,
"title": {"en-GB": "YOUR REPORT"},
"description": {"en-GB": ""},
"options": [
{"optionId": 0, "text": {"en-GB": "Ask me"}},
{"optionId": 1, "text": {"en-GB": "Phone a nurse"}},
{"optionId": 2, "text": {"en-GB": "Download full report"}},
],
"_links": {
"self": {
"method": "GET",
"href": "/assessments/898d915e-229f-48f2-9b98-cfd760ba8965",
},
"report": {
"method": "GET",
"href": "/reports/17340f51604cb35bd2c6b7b9b16f3aec",
},
},
}
I then url encode it like so and redirect to a report view:
url = reverse("my-reports")
reverse_url = encodeurl(data, url)
The urlencode output is returned as this:
"/api/v2/ada/reports?cardType=CHOICE&step=40&title="
"%7B%27en-GB%27%3A+%27YOUR+REPORT%27%7D&description="
"%7B%27en-GB%27%3A+%27%27%7D&options="
"%5B%7B%27optionId%27%3A+0%2C+%27text%27%3A+%7B%27en-GB"
"%27%3A+%27Ask+me%27%7D%7D%2C+%7B%27optionId%27%"
"3A+1%2C+%27text%27%3A+%7B%27en-GB%27%3A+%27Phone+a+nurse"
"%27%7D%7D%2C+%7B%27optionId%27%3A+2%2C+%27text%27%3A+%7B"
"%27en-GB%27%3A+%27Download+full+report%27%7D%7D%5D&_links="
"%7B%27self%27%3A+%7B%27method%27%3A+%27GET%27%2C+%27href%27%"
"3A+%27%2Fassessments%2F898d915e-229f-48f2-9b98-cfd760ba8965"
"%27%7D%2C+%27report%27%3A+%7B%27method%27%3A+%27GET%27%"
"2C+%27href%27%3A+%27%2Freports%2F"
"17340f51604cb35bd2c6b7b9b16f3aec%27%7D%7D"
In my report view I get the payload from the url query string:
class Reports(generics.GenericAPIView):
permission_classes = (permissions.AllowAny,)
def get(self, request, *args, **kwargs):
result = request.GET
data = result.dict()
print(data)
Now the problem is that the output of the transferred data which is printed above has quotes for nested keys.
{
'cardType': 'CHOICE',
'step': '40', 'title': "{'en-GB': 'YOUR REPORT'}",
'description': "{'en-GB': ''}",
'options': "[{'optionId': 0,
'text': {'en-GB': 'Ask me'}},
{'optionId': 1, 'text': {'en-GB': 'Phone a nurse'}}, {'optionId': 2, 'text': {'en-GB': 'Download full report'}}]",
'_links': "{'self': {'method': 'GET', 'href': '/assessments/898d915e-229f-48f2-9b98-cfd760ba8965'}, 'report': {'method': 'GET', 'href': '/reports/17340f51604cb35bd2c6b7b9b16f3aec'}}"
}
Notice the double quote in the "description", "options" keys etc.
Is there a way to remove this or pass this across to the view so that when I unwrap it I have the same data I started with.
Thanks

import urllib.parse
import json
# convert your dict to a string
json_string = json.dumps(your_payload)
# urlencode the json string
encoded = urllib.parse.quote(json_string.encode('utf-8'))
# build the url
url = reverse("my-reports")
reverse_url = f"{url}?payload={encoded}"
Then in your view:
class Reports(generics.GenericAPIView):
permission_classes = (permissions.AllowAny,)
def get(self, request, *args, **kwargs):
payload = request.GET.get('payload')
# do the encoding process in reverse
urldecoded = urllib.parse.unquote(payload)
data = json_loads(urldecoded)
print(data)

Related

How to use Zeep to skip first elements and only pass second elements in Python

I have SOAP request example below. Would like to skip "Request_References" elements and only pass "Response_Filter" using Zeep
<?xml version="1.0" encoding="UTF-8"?>
<wd:Get_Workers_Request xmlns:wd="urn:com.workday/bsvc" wd:version="v38.0">
<wd:Request_References
wd:Skip_Non_Existing_Instances="true"
wd:Ignore_Invalid_References="true">
<wd:Worker_Reference>
<wd:ID wd:type="Employee_ID">abcdef</wd:ID>
</wd:Worker_Reference>
</wd:Request_References>
<wd:Response_Filter>
<wd:As_Of_Effective_Date>2022-09-03</wd:As_Of_Effective_Date>
<wd:As_Of_Entry_DateTime>2022-09-03T14:26:34</wd:As_Of_Entry_DateTime>
<wd:Page>1</wd:Page>
<wd:Count>100</wd:Count>
</wd:Response_Filter>
</wd:Get_Workers_Request>
If I pass request_dict as below, it works
request_dict = {
'Worker_Reference': {
'ID': {
'type': 'Employee_ID',
'_value_1': employee_id
},
'Descriptor': None
},
'Skip_Non_Existing_Instances': None,
'Ignore_Invalid_References': None
}
If I want to skip "Request_References" elements using code below, then it doesn't work.
client = Client(url, wsse=UsernameToken(user, password), plugins=[history])
request_dict = {
'Request_References': xsd.SkipValue,
'Response_Filter': {
'Page': '1',
'Count': '1'
}
}
client.service.Get_Workers(request_dict)
Reall
Figured it out. Here is the answer
request_dict = {
'Worker_Reference': {
'ID': {
'type': 'Employee_ID',
'_value_1': employee_id
},
'Descriptor': None
},
'Skip_Non_Existing_Instances': None,
'Ignore_Invalid_References': None
}
filter_dict = {
'Page':1,
'Count': 1
}
try :
print (client.service.Get_Workers(Request_References=request_dict,Response_Filter=filter_dict ))
except Exception:
pass

copying data from json response [Python]

I have a scenario where I am trying to extract data from json response which is obtained from the GET request and then rebuilding the json data by changing some values and then sending a PUT request at same time after rebuilding the json data(i.e, after changing idter value)
below is the target json response.
target_json = {
"name": "toggapp",
"ts": [
1234,
3456
],
"gs": [
{
"id": 4491,
"con": "mno"
},
{
"id": 4494,
"con": "hkl"
}
],
"idter": 500,
"datapart": false
}
from the above json I am trying to change the idter value to my custom value and rebuild it into json data again and post the new json data.
Here is what I have tried :
headers = {'Authorization': 'bearer ' + auth_token, 'Content-Type':'application/json', 'Accept':'application/json'}
tesstid =[7865, 7536, 7789]
requiredbdy = []
for key in testid:
get_metadata_targetjson= requests.get('https://myapp.com/%s' %key, headers = headers)
metadata=get_metadata_target.json()
for key1 in metadata:
requiredbdy.append(
{
"metadata" : [{
"name": key1['name'],
"ts": key1['ts'],
"gs": key1[gs],
"idter": 100, #custom value which I want to change
"datapart": false
} ]
}
)
send_metadata_newjson= requests.put('https://myapp.com/%s' %key, headers = headers data = requiredbdy)
print(send_metadata_newjson.status_code)
Is this approach fine or How do I proceed in order to achieve this scenario.
You can use the built-in json module for this like so
import json
my_json = """
{
"name": "toggapp",
"ts": [
1234,
3456
],
"gs": [
{
"id": 4491,
"con": "mno"
},
{
"id": 4494,
"con": "hkl"
}
],
"idter": 500,
"datapart": false
}
"""
json_obj = json.loads(my_json)
json_obj['idter'] = 600
print(json.dumps(json_obj))
Prints
{"name": "toggapp", "ts": [1234, 3456], "gs": [{"id": 4491, "con": "mno"}, {"id": 4494, "con": "hkl"}], "idter": 600, "datapart": false}
There's this small script used it to find entries in some very long and unnerving JSONs. not very beautifull und badly documented but maybe helps in your scenario.
from RecursiveSearch import Retriever
def alter_data(json_data, key, original, newval):
'''
Alter *all* values of said keys
'''
retr = Retriever(json_data)
for item_no, item in enumerate(retr.__track__(key)): # i.e. all 'value'
# Pick parent objects with a last element False in the __track__() result,
# indicating that `key` is either a dict key or a set element
if not item[-1]:
parent = retr.get_parent(key, item_no)
try:
if parent[key] == original:
parent[key] = newval
except TypeError:
# It's a set, this is not the key you're looking for
pass
if __name__ == '__main__':
alter_data(notification, key='value',
original = '********** THIS SHOULD BE UPDATED **********',
newval = '*UPDATED*')

Save dict of key and list of dict inside key to JSON where dictionary is stored by line

I have a similar question to this previous question. However, my dictionary has a structure like the following
data_dict = {
'refresh_count': 1,
'fetch_date': '10-10-2019',
'modified_date': '',
'data': [
{'date': '10-10-2019', 'title': 'Hello1'},
{'date': '11-10-2019', 'title': 'Hello2'}
]
}
I would like to store it in JSON so that my data is still stored in one dictionary per line. Something like:
{
'refresh_count': 1,
'fetch_date': '10-10-2019',
'modified_date': '',
'data': [
{'date': '10-10-2019', 'title': 'Hello1'},
{'date': '11-10-2019', 'title': 'Hello2'}
]
}
I cannot achieve it using simply using json.dumps (or dump) or the previous solution.
json.dumps(data_dict, indent=2)
>> {
"refresh_count": 1,
"fetch_date": "10-10-2019",
"modified_date": "",
"data": [
{
"date": "10-10-2019",
"title": "Hello1"
},
{
"date": "11-10-2019",
"title": "Hello2"
}
]
}
This is quite a hack, but you can implement a custom JSON encoder that will do what you want (see Custom JSON Encoder in Python With Precomputed Literal JSON). For any object that you do not want to be indented, wrap it with the NoIndent class. The custom JSON encoder will look for this type in the default() method and return a unique string (__N__) and store unindented JSON in self._literal. Later, in the call to encode(), these unique strings are replaced with the unindented JSON.
Note that you need to choose a string format that cannot possibly appear in the encoded data to avoid replacing something unintentionally.
import json
class NoIndent:
def __init__(self, o):
self.o = o
class MyEncoder(json.JSONEncoder):
def __init__(self, *args, **kwargs):
super(MyEncoder, self).__init__(*args, **kwargs)
self._literal = []
def default(self, o):
if isinstance(o, NoIndent):
i = len(self._literal)
self._literal.append(json.dumps(o.o))
return '__%d__' % i
else:
return super(MyEncoder, self).default(o)
def encode(self, o):
s = super(MyEncoder, self).encode(o)
for i, literal in enumerate(self._literal):
s = s.replace('"__%d__"' % i, literal)
return s
data_dict = {
'refresh_count': 1,
'fetch_date': '10-10-2019',
'modified_date': '',
'data': [
NoIndent({'date': '10-10-2019', 'title': 'Hello1'}),
NoIndent({'date': '11-10-2019', 'title': 'Hello2'}),
]
}
s = json.dumps(data_dict, indent=2, cls=MyEncoder)
print(s)
Intermediate representation returned by super(MyEncoder, self).encode(o):
{
"fetch_date": "10-10-2019",
"refresh_count": 1,
"data": [
"__0__",
"__1__"
],
"modified_date": ""
}
Final output:
{
"fetch_date": "10-10-2019",
"refresh_count": 1,
"data": [
{"date": "10-10-2019", "title": "Hello1"},
{"date": "11-10-2019", "title": "Hello2"}
],
"modified_date": ""
}

Formatting JSON data in Python

I am sending JSON data over post method to one of my ML models, the problem is
it is not able to pick which object to fetch for fields.
Below is the sample JSON
{
"a": {
"asD": 1553990400000,
"cust": "S65A00",
"party": "1234",
"custS": null,
"sngldt: 1557014400000,
},
"b": {
"History": [],
"cust": "S65A00",
"mb_cntry": "US",
"mbdt": 1490918400000,
"mbsg_dt": 1553904000000,
}
}
How Can I Merge this JSON in the ML Code in single braces like below, I don't have the luxury to format the JSON itself during Post request.
{
"asD": 1553990400000,
"cust": "S65A00",
"party": "1234",
"custS": null,
"sngldt: 1557014400000,
"History": [],
"mb_cntry": "US",
"mbdt": 1490918400000,
"mbsg_dt": 1553904000000,
}
Below is the code that I have tried but failing
#app.route('/', methods=['GET', 'POST'])
def execute():
if request.method == 'POST':
json_data = request.get_json()
batch=json.dumps(json_data)
dataFrame = pd.DataFrame(json_data)
print(len(dataFrame.columns))
df=pd.melt(dataFrame,id_vars=[' ','b'], value_name='values')
print(df)
merged = dict()
merged.update(obj.a)
merged.update(obj.b)
With unpacking syntax:
...
json_data = dict(json_data['a'], **json_data['b'])
print(json_data)
prints:
{'asD': 1553990400000, 'cust': 'S65A00', 'party': '1234', 'custS': 'null', 'sngldt': 1557014400000, 'History': [], 'mb_cntry': 'US', 'mbdt': 1490918400000, 'mbsg_dt': 1553904000000}

Github API get the last commit for every repository with a search term response

I'm working on a project using Python(3.6) in which I have implemented GitHub JSON APIs by using the Python's requests package.I'm getting the list of public repos for a specific serach term provided by user.It's providing a response object which includes information about repos but I need to get the last commit infor of every repo, how can I get this info from response object from Github api.
Here's how I have implemented this:
class GhNavigator(CreateView):
def get(self, request, *args, **kwargs):
term = request.GET.get('search_term')
username = 'arycloud'
token = 'API_TOKEN'
login = requests.get('https://api.github.com/search/repositories?q=' + term, auth=(username, token))
response = login.json()
print(response)
return render(request, 'navigator/template.html', {'response': response, 'term': term})
Here's a sample response:
{'total_count': 4618, 'incomplete_results': False, 'items': [{'id': 6750871, 'name': 'arrow', 'full_name': 'crsmithdev/arrow', 'owner': {'login': 'crsmithdev', 'id': 1596037, 'avatar_url': 'https://avatars1.githubusercontent.com/u/1596037?v=4', 'gravatar_id': '', 'url': 'https://api.github.com/users/crsmithdev', 'html_url': 'https://github.com/crsmithdev', 'followers_url': 'https://api.github.com/users/crsmithdev/followers', 'following_url': 'https://api.github.com/users/crsmithdev/following{/other_user}', 'gists_url': 'https://api.github.com/users/crsmithdev/gists{/gist_id}', 'starred_url': 'https://api.github.com/users/crsmithdev/starred{/owner}{/repo}', 'subscriptions_url': 'https://api.github.com/users/crsmithdev/subscriptions', 'organizations_url': 'https://api.github.com/users/crsmithdev/orgs', 'repos_url': 'https://api.github.com/users/crsmithdev/repos', 'events_url': 'https://api.github.com/users/crsmithdev/events{/privacy}', 'received_events_url': 'https://api.github.com/users/crsmithdev/received_events', 'type': 'User', 'site_admin': False}, 'private': False, 'html_url': 'https://github.com/crsmithdev/arrow', 'description': 'Better dates & times for Python', 'fork': False, 'url': 'https://api.github.com/repos/crsmithdev/arrow', 'forks_url': 'https://api.github.com/repos/crsmithdev/arrow/forks', 'keys_url': 'https://api.github.com/repos/crsmithdev/arrow/keys{/key_id}', 'collaborators_url': 'https://api.github.com/repos/crsmithdev/arrow/collaborators{/collaborator}', 'teams_url': 'https://api.github.com/repos/crsmithdev/arrow/teams', 'hooks_url': 'https://api.github.com/repos/crsmithdev/arrow/hooks', 'issue_events_url': 'https://api.github.com/repos/crsmithdev/arrow/issues/events{/number}', 'events_url': 'https://api.github.com/repos/crsmithdev/arrow/events', 'assignees_url': 'https://api.github.com/repos/crsmithdev/arrow/assignees{/user}', 'branches_url': 'https://api.github.com/repos/crsmithdev/arrow/branches{/branch}', 'tags_url': 'https://api.github.com/repos/crsmithdev/arrow/tags', 'blobs_url': 'https://api.github.com/repos/crsmithdev/arrow/git/blobs{/sha}', 'git_tags_url': 'https://api.github.com/repos/crsmithdev/arrow/git/tags{/sha}', 'git_refs_url': 'https://api.github.com/repos/crsmithdev/arrow/git/refs{/sha}', 'trees_url': 'https://api.github.com/repos/crsmithdev/arrow/git/trees{/sha}', 'statuses_url': 'https://api.github.com/repos/crsmithdev/arrow/statuses/{sha}', 'languages_url': 'https://api.github.com/repos/crsmithdev/arrow/languages', 'stargazers_url': 'https://api.github.com/repos/crsmithdev/arrow/stargazers', 'contributors_url': 'https://api.github.com/repos/crsmithdev/arrow/contributors', 'subscribers_url': 'https://api.github.com/repos/crsmithdev/arrow/subscribers', 'subscription_url': 'https://api.github.com/repos/crsmithdev/arrow/subscription', 'commits_url': 'https://api.github.com/repos/crsmithdev/arrow/commits{/sha}', 'git_commits_url': 'https://api.github.com/repos/crsmithdev/arrow/git/commits{/sha}', 'comments_url': 'https://api.github.com/repos/crsmithdev/arrow/comments{/number}', 'issue_comment_url': 'https://api.github.com/repos/crsmithdev/arrow/issues/comments{/number}', 'contents_url': 'https://api.github.com/repos/crsmithdev/arrow/contents/{+path}', 'compare_url': 'https://api.github.com/repos/crsmithdev/arrow/compare/{base}...{head}', 'merges_url': 'https://api.github.com/repos/crsmithdev/arrow/merges', 'archive_url': 'https://api.github.com/repos/crsmithdev/arrow/{archive_format}{/ref}', 'downloads_url': 'https://api.github.com/repos/crsmithdev/arrow/downloads', 'issues_url': 'https://api.github.com/repos/crsmithdev/arrow/issues{/number}', 'pulls_url': 'https://api.github.com/repos/crsmithdev/arrow/pulls{/number}', 'milestones_url': 'https://api.github.com/repos/crsmithdev/arrow/milestones{/number}', 'notifications_url': 'https://api.github.com/repos/crsmithdev/arrow/notifications{?since,all,participating}', 'labels_url': 'https://api.github.com/repos/crsmithdev/arrow/labels{/name}', 'releases_url': 'https://api.github.com/repos/crsmithdev/arrow/releases{/id}', 'deployments_url': 'https://api.github.com/repos/crsmithdev/arrow/deployments', 'created_at': '2012-11-18T20:23:27Z', 'updated_at': '2018-05-24T11:23:14Z', 'pushed_at': '2018-05-21T09:03:24Z', 'git_url': 'git://github.com/crsmithdev/arrow.git', 'ssh_url': 'git#github.com:crsmithdev/arrow.git', 'clone_url': 'https://github.com/crsmithdev/arrow.git', 'svn_url': 'https://github.com/crsmithdev/arrow', 'homepage': 'https://arrow.readthedocs.org', 'size': 1454, 'stargazers_count': 4999, 'watchers_count': 4999, 'language': 'Python', 'has_issues': True, 'has_projects': True, 'has_downloads': True, 'has_wiki': True, 'has_pages': True, 'forks_count': 416, 'mirror_url': None, 'archived': False, 'open_issues_count': 123, 'license': {'key': 'other', 'name': 'Other', 'spdx_id': None, 'url': None}, 'forks': 416, 'open_issues': 123, 'watchers': 4999, 'default_branch': 'master', 'permissions': {'admin': False, 'push': False, 'pull': True}, 'score': 126.08792}]}
Get the last commit from multiple repositories
Use GraphQL API v4 to perform your search request and get the most recent commit using history(first: 1) when iterating the repository. The graphQL query :
{
search(query: "language:python", type: REPOSITORY, first: 100) {
edges {
node {
... on Repository {
defaultBranchRef {
target {
... on Commit {
history(first: 1) {
nodes {
message
committedDate
authoredDate
oid
author {
email
name
}
}
}
}
}
}
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
Try it in the explorer
In python :
import json
import requests
access_token = "YOUR_TOKEN"
query = """
{
search(query: "language:python", type: REPOSITORY, first: 100) {
edges {
node {
... on Repository {
defaultBranchRef {
target {
... on Commit {
history(first: 1) {
nodes {
message
committedDate
authoredDate
oid
author {
email
name
}
}
}
}
}
}
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
"""
data = {'query': query.replace('\n', ' ')}
headers = {'Authorization': 'token ' + access_token, 'Content-Type': 'application/json'}
r = requests.post('https://api.github.com/graphql', headers=headers, json=data)
print(json.loads(r.text)['data']['search']['edges'])
You will then need to go through pagination with the cursor value specifying after: "END_CURSOR_VALUE" if hasNextPage is true. check this
Get the last commit from a single repository
You can use list commits on a repository API and only return the first element with per_page=1 since the first is the most recent one. If you don't specify the sha parameter it will take the default branch :
https://api.github.com/repos/torvalds/linux/commits?per_page=1
Using Rest API v3 :
import requests
repo = 'torvalds/linux'
r = requests.get('https://api.github.com/repos/{0}/commits?per_page=1'.format(repo))
commit = r.json()[0]["commit"]
print(commit)
And if you want to use GraphQL API v4, you can do the following :
import json
import requests
access_token = "YOUR_TOKEN"
query = """
{
repository(owner: "torvalds", name: "linux") {
defaultBranchRef {
target {
... on Commit {
history(first: 1) {
nodes {
message
committedDate
authoredDate
oid
author {
email
name
}
}
}
}
}
}
}
}
"""
data = {'query': query.replace('\n', ' ')}
headers = {'Authorization': 'token ' + access_token, 'Content-Type': 'application/json'}
r = requests.post('https://api.github.com/graphql', headers=headers, json=data)
print(json.loads(r.text)['data']['repository']['defaultBranchRef']['target']['history']['nodes'][0])
Try it in the explorer
You can also consider the last push event: that would represent the last and most recent commit done (on any branch), pushed by a user to this repo.

Categories

Resources