Part 1 (solved from the help indicated in the comments of this question):
I would like to open this JSON response print() in a page of my default browser (Chrome), as I have a JSON Viewer extension and I would like to study this data with an easier view than in the Visual Studio Code terminal.
Is there any simple method for this to be done without the need to create an html file with this data, create a specific server and only then be able to open it in the browser?
I tried to use the webbrowser but it just opened any page, it didn't open the data.
import requests
import json
import webbrowser
url="https://api.betfair.com/exchange/betting/json-rpc/v1"
header = { 'X-Application' : 'AAAAAAAAAAAAA', 'X-Authentication' : 'BBBBBBBBBBBB' ,'content-type' : 'application/json' }
jsonrpc_req='{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/listCompetitions", "params": {"filter":{"eventTypeIds": [1]}}, "id": 2}'
response = requests.post(url, data=jsonrpc_req, headers=header)
data = json.dumps(json.loads(response.text), indent=3)
webbrowser.open(data)
Part 2:
I'm trying to open a .json
file by the browser so that I can analyze the data more easily, according to the tips in the comments, I started using the webbrowser
, but when I try to open .json
the page doesn't open and when I try to open it as .txt
or .html
the extension that improves the visualization of JSON
in the browser, doesn't recognize the data and doesn't format for it.
How could I manage to do this?
My extension in Chrome:
https://chrome.google.com/webstore/detail/json-viewer/gbmdgpbipfallnflgajpaliibnhdgobh?hl=pt-BR
Github project extension:
https://github.com/tulios/json-viewer
I post my Python
code here just to make it easier to see print()
:
import requests
import json
url="https://api.betfair.com/exchange/betting/json-rpc/v1"
header = { 'X-Application' : 'AAAAAAAAAAAAA', 'X-Authentication' : 'BBBBBBBBBBBB' ,'content-type' : 'application/json' }
jsonrpc_req='{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/listCompetitions", "params": {"filter":{"eventTypeIds": [1]}}, "id": 2}'
response = requests.post(url, data=jsonrpc_req, headers=header)
with open("ApiBetfair.html", "w+", newline="", encoding="UTF-8") as f:
data = json.dumps(json.loads(response.text), indent=3)
f.write(data)
new = 2
webbrowser.open('file://' + os.path.realpath('ApiBetfair.html'),new=new)