How to extract large json file to csv using Python - json

I'm trying to convert a very large .json file to a .csv file. Here is a sample of the json file I have been using.
The file I'll be getting directly from a journal publisher in the same format.
The main purpose of this is to extract all the component from the .json file and put the information to our database.
Below is the code I have tried.
import csv, json, sys
if sys.argv[1] is not None and sys.argv[2] is not None:
fileInput = sys.argv[1]
fileOutput = sys.argv[2]
inputFile = open(fileInput, encoding="utf8") #open json file
outputFile = open(fileOutput, 'w') #load csv file
data = json.load(inputFile) #load json content
inputFile.close() #close the input file
output = csv.writer(outputFile) #create a csv.write
output.writerow(data[0].keys()) # header row
for row in data:
output.writerow(row.values()) #values row
I'm getting this error:
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 542)

that is not valid json. the opening bracket on byte offset 0 is closed with a closing bracket on byte offset 383, then another bracket is created on byte offset 386, the new backet outside of the closing bracket on offset 383 which is created on byte offset 386 is illegal in json, the only thing that would be legal after the closing bracket is whitespace (spaces, tabs, newlines)
it looks a lot like 100 separate json's that are all line-separated, though, but there is no easy way of parsing that, as valid jsons may also contain newlines. if the data provider can guarantee that their individual jsons NEVER contains newlines, or that all their newlines are encoded in some other way than using hex 0A bytes, for example encoded with hex 5C6E instead of hex 0A, then you could ofc split up the jsons by newlines.. but that approach is unreliable if the data provider's jsons may contain newlines. (and the json specificaion allows newlines, 0x0A bytes, in jsons, so that would require your data provider to only use a newline-lacking subset of json.. if your provider is looking for a quick-fix to this issue: use NULL-bytes, hex 00, as the separator instead of hex 0x0A, because json never contains null bytes, those always has to be encoded in json, to "\u0000", then you could reliably split up the jsons by null-bytes)
here is what happens when i try to parse all 100 lines as individual jsons, splitting them by the 0x0A byte, using the code:
<?php
$jsons=file_get_contents("https://pastebin.com/raw/p9NbH2tG");
json_decode($jsons);
echo json_last_error_msg(),PHP_EOL;
$jsons=explode("\n",$jsons);
foreach($jsons as $json){
json_decode($json);
echo json_last_error_msg(),PHP_EOL;
}
output:
$ php foo.php
Syntax error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
No error
as you can see, each individual line in your file contains valid json, but as a whole, it's not valid json. but splitting them by newlines is NOT a reliable way, it just happens to work here because there are no newlines in any of the 100 jsons in your test file.

This looks a lot like the question asked here Django convert JSON to CSV
Can you share a sample of the json response you are getting? Perhaps there is an issue with attempting to decode multiple dictionaries etc.

Related

When i import my JSON into my wallet it says invalid, so I'm using JSONLint and it gives Error: Parse error on line 9: Expecting 'STRING', got 'EOF'

This is how the code is with all its quotation marks placed below
{"address":"abc123a0b123b123ab12345b1abcd12345d12a123","id":"12ab1234-12b1-12ab-1234-1ab12345abc1","version":3,"crypto":{"cipher":"abc-123-abc","cipherparams":{"iv":"ab1234abcd123a1bc12a123a1234b12"},
Ur json has invalid format. Try this resource for checking JSONs.

Snowflake throwing error (Error parsing JSON: misplaced { )

I am trying to load json files in to snow flake using copy command.I have two files of same structure.However one file loaded without issue,the other one is throwing the error
"Error parsing JSON: misplaced { "
The simple example select parse_json($1) record from values ('{{'); also errors with Error parsing JSON: misplaced {, pos 2 so your second file probably does in fact contain invalid JSON.
Try running the statement in validation mode (e.g. copy into mytable validation_mode = 'RETURN_ERRORS';) which will return a table containing useful troubleshooting info like the line number and character of the error(s).
The docs cover this here: https://docs.snowflake.com/en/sql-reference/sql/copy-into-table.html#validating-staged-files

Error while reading JSON file with Python

I am getting an error while reading JSON file using python's json module and I'm not able to understand what is wrong. Below are my files and code for your reference:
json.load(files_lst[1])
error:
AttributeError: 'str' object has no attribute 'read'
After reading several answers I also tried:
json.loads(files_lst[1])
but I get the following error:
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
What is wrong here? Thanks a lot for your help
>>> files_lst
['All_Transcripts/x3021_10.50.48.111_04-04-2019.json',
'All_Transcripts/x5363_09.33.36.955_08-27-2019.mp3.json',
'All_Transcripts/x3580_11.35.53.462_05-13-2019.json',
'All_Transcripts/x4342_08.55.01.523_08-01-2019.json',
'All_Transcripts/x9496_15.26.32.382_05-21-2019.json',
'All_Transcripts/x5374_08.38.15.692_06-17-2019.json',
'All_Transcripts/x4342_13.43.57.128_03-21-2019.json']
json.load accepts an open file descriptor. So you should call it like this:
json.load(open(files_lst[1]))
json.loads accepts a json as a string. So you should call it like this:
json.loads(open(files_lst[1]).read())

What does this error code mean? JSONDecodeError: Expecting Value

I'm using discord.py to create a bot for my server. I'm following a tutorial made by Spyros, and I'm getting this error:
JSONDecodeError: Expecting value: line 1 column 1 (char 0)]
It's just an empty JSON file, just curly brackets with no data.
Anyone knows what I can do? I'll post more code if needed, thanks!
JSONDecode is expecting data and as you said, you are passing an empty JSON "file"

Invalid characters with fromJSON function

I am trying to import json file into R. For the purpose I am using packages such as rjson, jsonlite, RJSONIO, etc. I have tried different things as shown below but I got errors with all of them.
mydata <- fromJSON(paste(readLines("result_prod_14-15_08_17.json"), collapse=""))
The error I got is:
Error: lexical error: invalid char in json text.
{ "_id" : ObjectId("59920f495401ac79452a0
(right here) ------^
Using 'RJSONIO' package:
md <- stream_in(file("result_prod_14-15_08_17.json"))
produces the following error:
opening fileconnectionoldClass input connection.
Error: parse error: premature EOF
{
(right here) ------^
closing fileconnectionoldClass input connection.
It must be something straightforward but since I am new to R I struggle to figure it out.