Python code to Download Specific JSON key value data through REST API calls

2024/11/19 6:23:06

I am trying to write a code in python which download only specific key value in the Calls.

So the solution might be

  1. Downloading the Raw data and later removing the unwanted through regex or 2)Applying specific process in API calls itself which downloads and appends only "Value".

Note - Code for access_token not included.

My Basic code from postman was

url = "https://xyz-stg.csod.com/services/api/x/odata/api/views/vw_rpt_performance_comment"payload={}
headers = {'Authorization': 'Bearer ' + access_token,'Cookie': 'ASP.NET_SessionId=hsylq0qqp; Cookie_2=value'
}response = requests.request("GET", url, headers=headers, data=payload).json()print(response, file=open(r"C:\Users\path\Desktop\Python script\output.json", 'w', encoding='utf-8', errors='ignore'))while response['@odata.nextLink']:url = response['@odata.nextLink']response = requests.request("GET", url, headers=headers, data=payload).json()#response.extend(response)print(response, file=open(r"C:\Users\path\Desktop\Python script\output.json", 'a', encoding='utf-8', errors='ignore'))break

Raw json output data

*{ "@odata.context": "https://example.com/services/api/x/odata/api/views/$metadata#vw_rpt_review_response_comment", "value": [ { "pr_comment_id": 1, "pr_comment": "Test Comment" }, { "pr_comment_id": 2, "pr_comment": "Test Comment" }, { "pr_comment_id": 3, "pr_comment": "Test Comment" } ],
"@odata.nextLink": "https://example.com/services/api/x/odata/api/views/$metadata#vw_rpt_review_response_comment?$skip=1000"
} { "@odata.context": "https://example.com/services/api/x/odata/api/views/$metadata#vw_rpt_review_response_comment", "value": [ { "pr_comment_id": 4, "pr_comment": "Test Comment" }, { "pr_comment_id": 5, "pr_comment": "Test Comment" }, { "pr_comment_id": 6, "pr_comment": "Test Comment" } ],
"@odata.nextLink": "https://example.com/services/api/x/odata/api/views/$metadata#vw_rpt_review_response_comment?$skip=2000"
}*

Beautify json

REQUIRED OUTPUT

*{ "value": [ { "pr_comment_id": 1, "pr_comment": "Test Comment" }, { "pr_comment_id": 2, "pr_comment": "Test Comment" }, { "pr_comment_id": 3, "pr_comment": "Test Comment" } , { "pr_comment_id": 4, "pr_comment": "Test Comment" }, { "pr_comment_id": 5, "pr_comment": "Test Comment" }, { "pr_comment_id": 6, "pr_comment": "Test Comment" } ]
}*

Beautify json

Answer

As your individual response come through, you can create a list of the values using extend (vs append). Then create your final dictionary.

Here's a mockup of one way to implement -collect all the response, then iterate over the list. Otherwise you can parse each response as they come in.

response1 = { "@odata.context": "https://example.com/services/api/x/odata/api/views/$metadata#vw_rpt_review_response_comment", "value": [ { "pr_comment_id": 1, "pr_comment": "Test Comment" }, { "pr_comment_id": 2, "pr_comment": "Test Comment" }, { "pr_comment_id": 3, "pr_comment": "Test Comment" } ],
"@odata.nextLink": "https://example.com/services/api/x/odata/api/views/$metadata#vw_rpt_review_response_comment?$skip=1000"
} response2 = { "@odata.context": "https://example.com/services/api/x/odata/api/views/$metadata#vw_rpt_review_response_comment", "value": [ { "pr_comment_id": 4, "pr_comment": "Test Comment" }, { "pr_comment_id": 5, "pr_comment": "Test Comment" }, { "pr_comment_id": 6, "pr_comment": "Test Comment" } ],
"@odata.nextLink": "https://example.com/services/api/x/odata/api/views/$metadata#vw_rpt_review_response_comment?$skip=2000"
}value_list = []
for resp in [response1, response2]:# print(resp['value'])value_list.extend(resp['value'])my_dict = {'value': value_list}
my_dict

Output

{'value': [{'pr_comment_id': 1, 'pr_comment': 'Test Comment'},{'pr_comment_id': 2, 'pr_comment': 'Test Comment'},{'pr_comment_id': 3, 'pr_comment': 'Test Comment'},{'pr_comment_id': 4, 'pr_comment': 'Test Comment'},{'pr_comment_id': 5, 'pr_comment': 'Test Comment'},{'pr_comment_id': 6, 'pr_comment': 'Test Comment'}]}
https://en.xdnf.cn/q/119984.html

Related Q&A

How to measure pairwise distances between two sets of points?

I have two datasets (csv files). Both of them contains latitudes-longitudes of two sets (220 and 4400) of points. Now I want to measure pairwise distances (miles) between these two sets of points (220 …

Interactively Re-color Bars in Matplotlib Bar Chart using Confidence Intervals

Trying to shade the bars in this chart based on the confidence that a selected y-value (represented by the red line) lies within a confidence interval. See recolorBars() method in the class example bel…

Unlock password protected Workbook using VBA or Python

I have a workbook name m.xlsx, but its password protected and Ive forgotten the password. How can I open it or un-protect it?The following code does not work:Unprotect workbook without password I need…

How do I make a variable detect if it is greater than or less than another one?

I am currently learning Python, and I decided to build a small "Guess the Number" type of game. I am using the random feature, and trying to make it so it will detect if the users input is eq…

Python Regular Expression from File

I want to extract lines following some sequence from a file. E.g. a file contains many lines and I want line in sequencejourney (a,b) from station south chennai to station punjab chandigarh journey (c,…

Changing the words keeping its meaning intact [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

How to create index for a SQLite3 database using SQLAlchemy?

I have multiple SQLite3 databases for which the models are not available. def index_db(name, tempdb):print(f{name.ljust(padding)} Indexing file: {tempdb})if tempdb.endswith(primary.sqlite):conn = sqlit…

Implementing ast.literal_eval on a numpy array

With the following expression, you can convert a string to a python dict.>>> import ast >>> a = ast.literal_eval("{muffin : lolz, foo : kitty}") >>> a {muffin: lolz…

Best way to make argument parser accept absolute number and percentage?

I am trying to write a Nagios style check to use with Nagios. I have working script that takes in something like -w 15 -c 10 and interprets that as "Warning at 15%, Critical at 10%". But I ju…

Python calculating prime numbers

I have to define a function called is_prime that takes a number x as input, then for each number n from 2 to x - 1, test if x is evenly divisible by n. If it is, return False. If none of them are, then…