I am trying to do the following. I have a url that links to a JSON file containing numerical data. I would like to read this data into a DataFrame. I have been following the advice here on how to read from a url into a JSON object. But, after running this code:
resp = HTTP.get("[url here]")
str = String(resp.body) jobj =
JSON.Parser.parse(str)
I get this error:
Expected 'r' here
Line: 0
Around: ...timestamp,open,high,lo...
_________^
Note, this code does run for a different JSON file that I used to test it. Next, after this step, I have tried following this advice on how to read a JSON file into a DataFrame. However, after running this code:
df = DataFrame(jsontable(jobj))
I get this error:
MethodError: no method matching read(::Dict{String,Any})
This step does not work on file I used to test the previous code. I appreciate any help with the above code or any new approaches to do this. Also I should mention I am very much a beginner when it comes to coding anything outside of data analysis. Thank you for any help!
EDIT: The first part of the code now runs correctly (I had the wrong URL). And I am getting a new error when I run the second part of the code. After running:
df = DataFrame(jsontable(jobj))
I get the error:
MethodError: no method matching read(::Dict{String,Any})
UPDATE: I ended up writing a function that is able to parse the file. It only extracts two of the columns but for now that works.
function json_df_parse(jobj)
timestamp = collect(keys(jobj));close = String[];
for key in timestamp
jobj2 = jobj[key]
push!(close,jobj2["4. close"])
end
df = DataFrame(timestamp = timestamp, close = close)
return df
end
Here is a sample of what the JSON file looks like:
{
"Meta Data": {
"1. Information": "Intraday (1min) open, high, low, close prices and volume",
"2. Symbol": "XXXX",
"3. Last Refreshed": "2021-02-22 20:00:00",
"4. Interval": "1min",
"5. Output Size": "Full size",
"6. Time Zone": "US/Eastern"
},
"Time Series (1min)": {
"2021-02-22 20:00:00": {
"1. open": "4.2800",
"2. high": "4.2800",
"3. low": "4.2800",
"4. close": "4.2800",
"5. volume": "149"
},
"2021-02-22 19:59:00": {
"1. open": "4.2400",
"2. high": "4.2500",
"3. low": "4.2400",
"4. close": "4.2500",
"5. volume": "999"