0

What I'm trying to do here is to open values.json (which is https://www.rolimons.com/itemapi/itemdetails) but I get error on

values = json.load(file)

    
r = requests.get("https://www.rolimons.com/itemapi/itemdetails")      
with open("values.json", "r+") as f:
  f.write(r.text)
f.close()

file = open("values.json")
values = json.load(file)
Trym
  • 15
  • 6
  • Did you try to load values from string inline in script? – 4EACH Feb 19 '22 at 18:22
  • 1
    The JSON as I download it now seems to be valid. Why are you writing it to a file? Why use `with` in one case but not in the other? – Thomas Weller Feb 19 '22 at 18:23
  • 1
    How did you create `"values.json"`? Did you mean to use `r.content`. This [question](https://stackoverflow.com/questions/17518937/saving-a-json-file-to-computer-python) seems relevant. – quamrana Feb 19 '22 at 18:24
  • With "r+" the file will not be created if it does not exist yet. – Thomas Weller Feb 19 '22 at 18:26

1 Answers1

0

This seems to work:

import requests
import json
req = requests.get("https://www.rolimons.com/itemapi/itemdetails")
values = json.loads(req.content)
print(values)
C. Pappy
  • 739
  • 4
  • 13
  • 2
    While this code may answer the question, it would be better to include some context, explaining _how_ it works and _when_ to use it. Code-only answers are not useful in the long run. – martineau Feb 19 '22 at 18:49