0

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)
Digital Farmer
  • 1,705
  • 5
  • 17
  • 67
  • 1
    You could create like a custom stream object that handles opening the browser and displaying the data pushed to it, then pass that as the `file=` argument to `print`. I just wouldn't use `print` here though. It isn't the right tool for the job. – Carcigenicate Jul 24 '21 at 18:09
  • 1
    You could store the JSON as a file and then open it in your browser using the [`webbrowser`](https://docs.python.org/3/library/webbrowser.html) module. Take a look at [this answer](https://stackoverflow.com/a/40905794/15873043), it should work for JSON as well. – fsimonjetz Jul 24 '21 at 18:18
  • 1
    write the data to a file and open that file in the browser – rioV8 Jul 24 '21 at 18:19
  • 1
    This seems like a great use-case for something like Flask – Anon Coward Jul 24 '21 at 18:20
  • Hello @fsimonjetz how are you? Thank you very much for your comment, however when trying to open a file with .json format the browser does not open the page and when trying to open as .html the extensions that format the page to read JSON do not work. Would you help me? I will update the question with new editions! Extension I use (https://chrome.google.com/webstore/detail/json-viewer/gbmdgpbipfallnflgajpaliibnhdgobh?hl=pt-BR) – Digital Farmer Jul 24 '21 at 18:38
  • 1
    @BrondbyIF Did you enable the local file option? On the [github page](https://github.com/tulios/json-viewer) of the add-on it says "Works on local files (if you enable this in chrome://extensions)". – fsimonjetz Jul 24 '21 at 18:43
  • 1
    Hi @fsimonjetz That was exactly it, I hesitated a lot in this detail, thank you very much! Could you create a short answer commenting on the two parts that helped me in the complete solution so that I can mark as a solution so as not to leave the question open? Thanks in advance. I don't want to delete the question because maybe someone else will need it in the future. – Digital Farmer Jul 24 '21 at 18:49
  • 1
    @BrondbyIF Glad to help and thank you, I appreciate it :) – fsimonjetz Jul 24 '21 at 19:01

1 Answers1

1

You could store the JSON in a local file and open it with the webbrowser module. As we figured out in the comments above, it is necessary to enable local file support in chrome://extensions for the extension to work properly.

import webbrowser

fp = "ApiBetfair.html"

with open(fp, "w+", newline="", encoding="UTF-8") as f:
    data = json.dumps(json.loads(response.text), indent=3)
    f.write(data)

new = 2 # open in new tab
webbrowser.open('file://' + os.path.realpath(fp),new=new)

fsimonjetz
  • 5,644
  • 3
  • 5
  • 21