Reading JSON from a file
The json.load()
method (without "s" in "load") can read a file directly:
import json
with open('strings.json') as f:
d = json.load(f)
print(d)
You were using the json.loads()
method, which is used for string arguments only.
The error you get with json.loads
is a totally different problem. In that case, there is some invalid JSON content in that file. For that, I would recommend running the file through a JSON validator.
There are also solutions for fixing JSON like for example How do I automatically fix an invalid JSON string?.
Using FileReader to read a JSON file?
The code at the question uses FileReader
incorrectly.
FileReader
.readAs<Type>
operation is asynchronous. FileReader
has load
and loadend
events where the result
property of event.target
and FileReader
instance is the resulting asynchronously processed data.
Do not parse the FileReader
object itself.
.readAs<Type>
expects a Blob
to be passed as parameter, not a JavaScript plain object.
const MY_JSON_FILE = [{ "hello": "world"}];
let json = JSON.stringify(MY_JSON_FILE);
const blob = new Blob([json], {type:"application/json"});
const fr = new FileReader();
fr.addEventListener("load", e => { console.log(e.target.result, JSON.parse(fr.result))});
fr.readAsText(blob);
How to read an external local JSON file in JavaScript?
You cannot make a AJAX call to a local resource as the request is made using HTTP.
A workaround is to run a local webserver, serve up the file and make the AJAX call to localhost.
In terms of helping you write code to read JSON, you should read the documentation for jQuery.getJSON()
:
http://api.jquery.com/jQuery.getJSON/
Read json file from resources and convert it into json string in JAVA
try(InputStream inputStream =Thread.currentThread().getContextClassLoader().getResourceAsStream(Constants.MessageInput)){
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readValue(inputStream ,
JsonNode.class);
json = mapper.writeValueAsString(jsonNode);
}
catch(Exception e){
throw new RuntimeException(e);
}
Parsing from a JSON file in Ruby and Extract numbers from Nested Hashes
You can use Array#map
to collect the reviews.
reviews = json['sentiment_analysis'][0]
positive_reviews = reviews['positive']
negative_reviews = reviews['negative']
positive_reviews.map { |review| review['score'] }
=> [0.6748984055823062, 0.6280145725181376]
negative_reviews.map { |review| review['score'] }
=> [-0.7923352042939829, -0.5734506634410159]
Hope this helps!
I can't read the JSON file and get data from it
this it's a basic storage method, this method is very bad for large json files but it's an example that show how can you do the job.
import os
import sys
import json
# storage.py key_name value
key =sys.argv[1]
value = sys.argv[2]
data_path = "data.json"
if os.path.isfile(data_path):
with open("data.json") as target:
json_data = json.load(target)
else:
json_data = {}
json_data[key] = value
with open("data.json", "w") as target:
json.dump(json_data, target)
in your case the problem is because the append flag when you open the file. If you need to write a new object you need to delete the last '}' of the json and add a ",object" item after that add the '}' char again.
Related Topics
Import a Module from a Relative Path
How to Expand the Output Display to See More Columns of a Pandas Dataframe
Find Unique Rows in Numpy.Array
Parsing Xml With Namespace in Python Via 'Elementtree'
How to Melt a Pandas Dataframe
Tkinter - Executing Functions Over Time
How to Sort a List of Objects Based on an Attribute of the Objects
Wait Until Page Is Loaded With Selenium Webdriver For Python
Scatter Plot With Different Text At Each Data Point
How to Write to an Excel Spreadsheet Using Python
Split Pandas Dataframe Based on Groupby
Numpy 'Logical_Or' For More Than Two Arguments