Mathematica Asked on March 25, 2021
Some people gave me a JSON file, which is exported from a camera. He asked me to parse the JSON file, and extract the value to each key. However, when I import the JSON file with:
Import[pathToTestJSON, "JSON"]
Mathematica warns: Import::jsonexpendofinput: Unexpected character found while looking for the end of input.
I searched the mathematica.Stackexchange.com, and found this post: Openning JSON file, (NDJSON, new line delimited) — big file where @Jason B. explained:
Import[myfile.ndjson,"JSON"] doesn’t work because the file as a whole is not valid JSON. But each line is valid JSON
But his solution is not for my situation, because my JSON record is not line-by-line, instead, its layout is a block. The following is a sample:
{
"ChannelInfo" : null,
"DevNo" : "DS-2CD2T86FWDV2-I8S20191216AACH",
"MecNo" : "100",
"MsgType" : 1000,
"Timestamp" : 1606910698042
}
(1) To take advantage of Openning JSON file, (NDJSON, new line delimited) — big file, how can I extract the JSON record block, instead of a line
, and then parse the JSON with Import[jsonRecordInBlock, "JSON"]
?
(2) If we do not copy the solution from Openning JSON file, (NDJSON, new line delimited) — big file, what is the solution?
(* Import as text first *)
text = Import["test.json", "Text"];
(* Split out the lines *)
lines = StringSplit[text, "n"];
(* Replace lines containing ONLY } with },
* then stitch the lines back together with newlines and remove the final comma.
* Finally, surround all objects with a JSON list [...], not braces. *)
newjson = "[n" <> StringDrop[StringJoin[lines /. "}" -> "},", "n"], -2] <> "]n"
(* Import the fixed string and decode as JSON *)
result = ImportString[newjson, "JSON"]
Correct answer by flinty on March 25, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP