1

Invalid Argument error while reading external json file's values in python

I tried:

import json

with open('https://www.w3schools.com/js/json_demo.txt') as json_file:
    data = json.load(json_file)
    #for p in data['people']:
    print('Name: ' + data['name'])

Gave me error:

with open('https://www.w3schools.com/js/json_demo.txt') as json_file: OSError: [Errno 22] Invalid argument: 'https://www.w3schools.com/js/json_demo.txt'

5

2 Answers 2

2

As open is for opening local files, not URLs as commented by jonrsharpe so, go with urllib as commented by fl00r.

Though the link provided by him was for python-2

Try this (python-3):

import json
from urllib.request import urlopen

with urlopen('https://www.w3schools.com/js/json_demo.txt') as json_file:
    data = json.load(json_file)
    #for p in data['people']:
    print('Name: ' + data['name'])

John

Sign up to request clarification or add additional context in comments.

Comments

2

Use requests

import requests
response = requests.get('https://www.w3schools.com/js/json_demo.txt')
response.encoding = "utf-8-sig"
data = response.json()
print(data['name'])
>>> John

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.