Python not reading valid JSON
python validate json file
python extract data from json file
python parse json array
python object to json
python print json
python string to json
I am scraping some HTML source from a web page to extract data stored in a json format
This is the Code:
url = 'https://finance.yahoo.com/quote/SPY' result = requests.get(url) c = result.content html = BeautifulSoup(c, 'html.parser') scripts = html.find_all('script') sl = for s in scripts: sl.append(s) s = (sl[-3]) s = s.contents s = str(s) s = s[119:-16] json_data = json.loads(s)
Running the above throws this error:
json.decoder.JSONDecodError: Expecting ',' delimiter: line 1 column 7506 (char7505)
When I take the content of variable s and pass it to a json formatter it's recognized as proper json.
I used the following web site to check the json: http://jsonprettyprint.com/json-pretty-printer.php
Why is this error coming up when using json.loads() in Python? I am assuming it has something to do with the string not being encoded properly or the presence of escape characters?
How do I solve this?
Your JSON contains certain unexpected tokens like
json.dumps first to resolve it.
print (json.dumps(s,indent =2)) s = json.dumps(s) json_data = json.loads(s)
Python Validate JSON Data, Check if a string is valid JSON in Python. Validate As we know, the json module provides two methods to parse JSON data using Python. We received the following error because the file content is not in JSON format. Python not reading valid JSON. Ask Question Asked 8 months ago. Active 8 months ago. Viewed 81 times 2. I am scraping some HTML source from a web page to extract data
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 7484 (char 7483)
Using the failure message, you can print a slice of the string to see where it is failing.
print(s[7400:7500]) mailboxes.isPrimary=\\"true\\" AND ymreq
As skaul05 stated, it is failing because of the
true token in the string.
Python json load(s) to parse JSON into Python dict, Understand use of Python's json.loads and load method to parse JSON. json. load() method (without “s” in “load”) used to read JSON encoded data from a which is used for parse valid JSON String into Python dictionary. The old version of JSON specified by the obsolete RFC 4627 required that the top-level value of a JSON text must be either a JSON object or array (Python dict or list), and could not be a JSON null, boolean, number, or string value.
import requests from bs4 import BeautifulSoup import json url = 'https://finance.yahoo.com/quote/SPY' result = requests.get(url) c = result.content html = BeautifulSoup(c, 'html.parser') scripts = html.find_all('script') sl = for s in scripts: sl.append(s) s = (sl[-3]) s = s.contents a = s[111:-12] jjjj = json.loads(a)
there's somethingrong when you deal with the list,you just use
json — JSON encoder and decoder — Python 3.8.5 documentation, Deserialize fp (a .read() -supporting text file or binary file containing a JSON JSONDecodeError will be raised if the given JSON document is not valid. Python JSON In this tutorial, you will learn to parse, read and write JSON in Python with the help of examples. Also, you will learn to convert JSON to dict and pretty print it.
If it was a valid JSON formatted text then the parser wouldn't complain. This is how I tested it
//first I scraped that page curl https://finance.yahoo.com/quote/SPY > SPY.json //then tried to parse it using json a = open("SPY.json") b = json.load(a) ValueError: No JSON object could be decoded
You probably need to first parse it in to valid xml first.
19.2. json — JSON encoder and decoder — Python 3.4.10 , The other arguments have the same meaning as in load(), except encoding which is ignored and deprecated. If the data being deserialized is not a valid JSON� The example above prints a JSON string, but it is not very easy to read, with no indentations and line breaks. The json.dumps() method has parameters to make it easier to read the result: Example
Python JSON: Encode(dump), Decode(load) json Data & File, Decoding JSON File or Parsing JSON file in Python; Compact if object is not complex raise TypeError(repr(object) + " is not JSON json.tool provides the command line interface to validate JSON pretty-print syntax. Reading and Writing JSON in Python (Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
- could u print your JSON before json.loads. You must be missing some kind of delimiter in there
- The json object is too big for me to post here as text
- why? it's just where you wrong? why give me such an evaluation?
- can you solve this problem by the method the second one provide? hehe