[{"data":1,"prerenderedAt":1221},["ShallowReactive",2],{"blog-post-en-read-json-file-python":3,"surround-\u002Fen\u002Fblog\u002Fread-json-file-python":1214},{"id":4,"title":5,"author":6,"body":7,"category":1192,"date":1193,"description":1194,"draft":1195,"extension":1196,"h1":5,"image":1197,"lastmod":1193,"locales":1198,"meta":1200,"navigation":1201,"path":1202,"promo":1203,"seo":1207,"stem":1208,"tags":1209,"__hash__":1213},"blog\u002Fen\u002Fblog\u002Fread-json-file-python.md","How to Read and Write JSON Files in Python","JSON Toolbox Team",{"type":8,"value":9,"toc":1167},"minimark",[10,14,36,46,49,54,160,184,196,202,206,212,215,268,272,278,281,350,371,377,383,387,390,396,403,411,414,420,438,442,452,460,471,477,480,486,489,515,519,525,528,597,611,638,653,657,662,668,674,682,689,693,696,779,782,889,893,899,940,947,952,955,959,964,984,988,994,1001,1005,1011,1015,1032,1036,1052,1056,1062,1066,1087,1091,1104,1108,1114,1118,1159,1162],[11,12,5],"h1",{"id":13},"how-to-read-and-write-json-files-in-python",[15,16,17,18,28,29],"p",{},"Python's ",[19,20,24],"a",{"href":21,"rel":22},"https:\u002F\u002Fdocs.python.org\u002F3\u002Flibrary\u002Fjson.html",[23],"nofollow",[25,26,27],"code",{},"json"," module has four functions that do almost everything you need, and one letter that decides which one to call: ",[30,31,32,35],"strong",{},[25,33,34],{},"s"," means string.",[37,38,44],"pre",{"className":39,"code":41,"language":42,"meta":43},[40],"language-python","json.load(f)      # read from a file object\njson.loads(text)  # read from a string\njson.dump(data, f)      # write to a file object\njson.dumps(data)        # write to a string\n","python","",[25,45,41],{"__ignoreMap":43},[15,47,48],{},"Once that clicks, the rest is details: which encoding to open with, how to pretty-print, which exceptions to catch, and what happens to your data when Python types become JSON types (and back).",[50,51,53],"h2",{"id":52},"the-four-functions-at-a-glance","The four functions at a glance",[55,56,57,76],"table",{},[58,59,60],"thead",{},[61,62,63,67,70,73],"tr",{},[64,65,66],"th",{},"Function",[64,68,69],{},"Input",[64,71,72],{},"Output",[64,74,75],{},"Use when",[77,78,79,103,128,144],"tbody",{},[61,80,81,87,93,96],{},[82,83,84],"td",{},[25,85,86],{},"json.load(fp)",[82,88,89,90],{},"File-like object with ",[25,91,92],{},".read()",[82,94,95],{},"Python object",[82,97,98,99,102],{},"Reading from a file, ",[25,100,101],{},"io.StringIO",", or a stream",[61,104,105,110,123,125],{},[82,106,107],{},[25,108,109],{},"json.loads(s)",[82,111,112,115,116,119,120],{},[25,113,114],{},"str",", ",[25,117,118],{},"bytes",", or ",[25,121,122],{},"bytearray",[82,124,95],{},[82,126,127],{},"Parsing a string you already have",[61,129,130,135,138,141],{},[82,131,132],{},[25,133,134],{},"json.dump(obj, fp)",[82,136,137],{},"Python object + file-like object",[82,139,140],{},"Writes to the file",[82,142,143],{},"Writing straight to disk",[61,145,146,151,153,157],{},[82,147,148],{},[25,149,150],{},"json.dumps(obj)",[82,152,95],{},[82,154,155],{},[25,156,114],{},[82,158,159],{},"You need the JSON as text",[15,161,162,163,165,166,169,170,173,174,176,177,169,180,183],{},"The ",[25,164,34],{}," suffix is the whole trick: ",[25,167,168],{},"loads"," and ",[25,171,172],{},"dumps"," work with ",[30,175,34],{},"trings; ",[25,178,179],{},"load",[25,181,182],{},"dump"," work with file objects.",[15,185,186,189,190,192,193,195],{},[25,187,188],{},"json.load()"," does not require a real file — anything with a ",[25,191,92],{}," method works, including ",[25,194,101],{},":",[37,197,200],{"className":198,"code":199,"language":42,"meta":43},[40],"import io\nimport json\n\ndata = json.load(io.StringIO('{\"name\": \"Ada\"}'))\n",[25,201,199],{"__ignoreMap":43},[50,203,205],{"id":204},"how-to-read-a-json-file-in-python","How to read a JSON file in Python",[37,207,210],{"className":208,"code":209,"language":42,"meta":43},[40],"import json\n\nwith open(\"data.json\", \"r\", encoding=\"utf-8\") as f:\n    data = json.load(f)\n\nprint(data[\"name\"])\n",[25,211,209],{"__ignoreMap":43},[15,213,214],{},"Three habits are worth fixing early:",[216,217,218,229,242],"ol",{},[219,220,221,228],"li",{},[30,222,223,224,227],{},"Use ",[25,225,226],{},"with","."," It closes the file even if parsing raises.",[219,230,231,237,238,241],{},[30,232,233,234,227],{},"Always pass ",[25,235,236],{},"encoding=\"utf-8\""," In text mode ",[25,239,240],{},"open()"," uses a locale-dependent default, which makes the same script behave differently on different machines. JSON is defined as UTF-8, so say so explicitly.",[219,243,244,247,248,115,251,115,254,115,256,115,259,115,262,119,265,227],{},[30,245,246],{},"The result is a plain Python object"," — usually a ",[25,249,250],{},"dict",[25,252,253],{},"list",[25,255,114],{},[25,257,258],{},"int",[25,260,261],{},"float",[25,263,264],{},"bool",[25,266,267],{},"None",[50,269,271],{"id":270},"how-to-write-json-to-a-file-in-python","How to write JSON to a file in Python",[37,273,276],{"className":274,"code":275,"language":42,"meta":43},[40],"import json\n\ndata = {\n    \"name\": \"Ada\",\n    \"city\": \"Zürich\",\n    \"active\": True,\n    \"score\": None,\n}\n\nwith open(\"data.json\", \"w\", encoding=\"utf-8\") as f:\n    json.dump(data, f, indent=2, ensure_ascii=False)\n",[25,277,275],{"__ignoreMap":43},[15,279,280],{},"The parameters that matter:",[55,282,283,293],{},[58,284,285],{},[61,286,287,290],{},[64,288,289],{},"Parameter",[64,291,292],{},"Effect",[77,294,295,305,323,333],{},[61,296,297,302],{},[82,298,299],{},[25,300,301],{},"indent=2",[82,303,304],{},"Pretty-prints with that many spaces. Omit it for compact output.",[61,306,307,312],{},[82,308,309],{},[25,310,311],{},"ensure_ascii=False",[82,313,314,315,318,319,322],{},"Writes ",[25,316,317],{},"Zürich"," instead of ",[25,320,321],{},"Z\\u00fcrich",". Non-ASCII text stays readable.",[61,324,325,330],{},[82,326,327],{},[25,328,329],{},"sort_keys=True",[82,331,332],{},"Sorts object keys alphabetically — useful for stable diffs.",[61,334,335,340],{},[82,336,337],{},[25,338,339],{},"separators=(\",\", \":\")",[82,341,342,343,346,347,349],{},"Removes the spaces ",[25,344,345],{},"indent"," would add; combine with no ",[25,348,345],{}," for the tightest output.",[15,351,352,355,356,359,360,363,364,367,368,370],{},[25,353,354],{},"ensure_ascii"," defaults to ",[25,357,358],{},"True",", which escapes every non-ASCII character. That is always valid JSON and always round-trips correctly, but a file full of ",[25,361,362],{},"\\u00fc"," sequences is unpleasant to read. Setting it to ",[25,365,366],{},"False"," requires the file to be written as UTF-8 — which is why the ",[25,369,236],{}," argument is not optional here.",[15,372,373,376],{},[30,374,375],{},"Writing atomically."," If another process may read the file while you write it, write to a temporary file and rename it, so a reader never sees a half-written document:",[37,378,381],{"className":379,"code":380,"language":42,"meta":43},[40],"import json\nimport os\n\ndef write_json_atomic(path, data, **kwargs):\n    tmp = f\"{path}.tmp\"\n    with open(tmp, \"w\", encoding=\"utf-8\") as f:\n        json.dump(data, f, **kwargs)\n        f.flush()\n        os.fsync(f.fileno())\n    os.replace(tmp, path)   # atomic within the same filesystem\n\nwrite_json_atomic(\"data.json\", data, indent=2, ensure_ascii=False)\n",[25,382,380],{"__ignoreMap":43},[50,384,386],{"id":385},"pretty-printing-json-in-python","Pretty-printing JSON in Python",[15,388,389],{},"To a string:",[37,391,394],{"className":392,"code":393,"language":42,"meta":43},[40],"text = json.dumps(data, indent=2, ensure_ascii=False)\nprint(text)\n",[25,395,393],{"__ignoreMap":43},[15,397,398,399,402],{},"To a file, use ",[25,400,401],{},"json.dump(data, f, indent=2)",". From the command line, Python ships a formatter:",[37,404,409],{"className":405,"code":407,"language":408,"meta":43},[406],"language-bash","python -m json.tool data.json            # pretty-print to stdout\npython -m json.tool --indent 4 data.json\npython -m json.tool --sort-keys data.json\n","bash",[25,410,407],{"__ignoreMap":43},[15,412,413],{},"For the tightest output:",[37,415,418],{"className":416,"code":417,"language":42,"meta":43},[40],"compact = json.dumps(data, separators=(\",\", \":\"))\n",[25,419,417],{"__ignoreMap":43},[15,421,422,423,426,427,115,429,115,431,433,434,437],{},"One caveat: ",[25,424,425],{},"pprint"," is for Python objects, not JSON. It prints ",[25,428,358],{},[25,430,366],{},[25,432,267],{},", and single quotes — none of which are valid JSON. Use ",[25,435,436],{},"json.dumps(..., indent=2)"," when the output has to be JSON.",[50,439,441],{"id":440},"encoding-pitfalls-utf-8-bom-and-utf-8-sig","Encoding pitfalls: UTF-8, BOM, and utf-8-sig",[15,443,444,445,448,449,451],{},"A JSON file saved by Notepad on Windows may start with a UTF-8 BOM (",[25,446,447],{},"\\xef\\xbb\\xbf","). Reading that with ",[25,450,236],{}," fails:",[37,453,458],{"className":454,"code":456,"language":457,"meta":43},[455],"language-text","JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1 (char 0)\n","text",[25,459,456],{"__ignoreMap":43},[15,461,462,463,466,467,470],{},"The error even tells you the fix. Use ",[25,464,465],{},"utf-8-sig",", which strips the BOM if present and behaves like ",[25,468,469],{},"utf-8"," otherwise:",[37,472,475],{"className":473,"code":474,"language":42,"meta":43},[40],"with open(\"data.json\", \"r\", encoding=\"utf-8-sig\") as f:\n    data = json.load(f)\n",[25,476,474],{"__ignoreMap":43},[15,478,479],{},"A helper that handles both cases is worth keeping around:",[37,481,484],{"className":482,"code":483,"language":42,"meta":43},[40],"def read_json(path):\n    \"\"\"Read JSON, tolerating a UTF-8 BOM.\"\"\"\n    with open(path, \"r\", encoding=\"utf-8-sig\") as f:\n        return json.load(f)\n",[25,485,483],{"__ignoreMap":43},[15,487,488],{},"Two related traps:",[490,491,492,502],"ul",{},[219,493,494,497,498,501],{},[30,495,496],{},"Mojibake"," (",[25,499,500],{},"ZÃ¼rich",") means the file was written in one encoding and read in another — usually UTF-8 bytes read as latin-1. Confirm the actual encoding rather than guessing.",[219,503,504,511,512,227],{},[30,505,506,507,510],{},"Garbled or ",[25,508,509],{},"\\u0000","-style output"," usually means the \"JSON\" file is not text at all — a binary file renamed to ",[25,513,514],{},".json",[50,516,518],{"id":517},"exception-handling","Exception handling",[37,520,523],{"className":521,"code":522,"language":42,"meta":43},[40],"import json\n\ntry:\n    with open(\"data.json\", \"r\", encoding=\"utf-8\") as f:\n        data = json.load(f)\nexcept FileNotFoundError:\n    print(\"data.json does not exist\")\nexcept PermissionError:\n    print(\"no permission to read data.json\")\nexcept json.JSONDecodeError as e:\n    print(f\"invalid JSON: {e.msg} (line {e.lineno}, column {e.colno})\")\n",[25,524,522],{"__ignoreMap":43},[15,526,527],{},"What each one tells you:",[55,529,530,540],{},[58,531,532],{},[61,533,534,537],{},[64,535,536],{},"Exception",[64,538,539],{},"Cause",[77,541,542,552,562,572,582],{},[61,543,544,549],{},[82,545,546],{},[25,547,548],{},"FileNotFoundError",[82,550,551],{},"Path does not exist",[61,553,554,559],{},[82,555,556],{},[25,557,558],{},"PermissionError",[82,560,561],{},"Process cannot read the file",[61,563,564,569],{},[82,565,566],{},[25,567,568],{},"json.JSONDecodeError",[82,570,571],{},"The text is not valid JSON",[61,573,574,579],{},[82,575,576],{},[25,577,578],{},"UnicodeDecodeError",[82,580,581],{},"The bytes are not valid in the encoding you chose",[61,583,584,590],{},[82,585,586,589],{},[25,587,588],{},"TypeError"," (on dump)",[82,591,592,593,596],{},"An object cannot be serialized (e.g. a ",[25,594,595],{},"set"," or a custom class)",[15,598,599,602,603,606,607,610],{},[25,600,601],{},"JSONDecodeError"," subclasses ",[25,604,605],{},"ValueError",", so ",[25,608,609],{},"except ValueError"," catches both it and other value problems. It carries useful attributes:",[490,612,613,623,632],{},[219,614,615,618,619,622],{},[25,616,617],{},"e.msg"," — the reason, e.g. ",[25,620,621],{},"Expecting value"," for an empty file",[219,624,625,115,628,631],{},[25,626,627],{},"e.lineno",[25,629,630],{},"e.colno"," — 1-based line and column",[219,633,634,637],{},[25,635,636],{},"e.pos"," — character offset",[15,639,640,641,643,644,648,649,227],{},"An empty or whitespace-only file produces ",[25,642,621],{}," — the Python equivalent of JavaScript's ",[645,646,647],"em",{},"Unexpected end of JSON input",". For a systematic walkthrough of parse failures, see ",[19,650,652],{"href":651},"\u002Fblog\u002Fjson-parse-error-debug","JSON Parse Failed: 10 Common API Errors and How to Debug Them",[50,654,656],{"id":655},"working-with-large-json-files","Working with large JSON files",[15,658,659,661],{},[25,660,188],{}," builds the whole document in memory. For a large file, that is the problem, not the parsing.",[15,663,664,667],{},[30,665,666],{},"JSON Lines"," is the practical fix when you control the format — one JSON value per line, read incrementally:",[37,669,672],{"className":670,"code":671,"language":42,"meta":43},[40],"import json\n\nwith open(\"events.jsonl\", \"r\", encoding=\"utf-8\") as f:\n    for line_number, line in enumerate(f, start=1):\n        line = line.strip()\n        if not line:\n            continue\n        try:\n            record = json.loads(line)\n        except json.JSONDecodeError as e:\n            print(f\"skipping line {line_number}: {e.msg}\")\n            continue\n        process(record)\n",[25,673,671],{"__ignoreMap":43},[15,675,676,677,681],{},"Peak memory is bounded by the largest single line, not the file size. See ",[19,678,680],{"href":679},"\u002Fblog\u002Fjson-vs-jsonl","JSON vs JSONL"," for when the format is worth adopting.",[15,683,684,685,688],{},"If you must read one huge JSON document (not JSON Lines), an incremental parser such as ",[25,686,687],{},"ijson"," yields items as it parses, instead of building the entire structure first. That is a third-party dependency, but it is the usual answer for multi-gigabyte single documents.",[50,690,692],{"id":691},"python-types-vs-json-types","Python types vs JSON types",[15,694,695],{},"The mapping is mostly automatic, with a few sharp edges:",[55,697,698,708],{},[58,699,700],{},[61,701,702,705],{},[64,703,704],{},"Python",[64,706,707],{},"JSON",[77,709,710,719,731,740,751,768],{},[61,711,712,716],{},[82,713,714],{},[25,715,250],{},[82,717,718],{},"object",[61,720,721,728],{},[82,722,723,115,725],{},[25,724,253],{},[25,726,727],{},"tuple",[82,729,730],{},"array",[61,732,733,737],{},[82,734,735],{},[25,736,114],{},[82,738,739],{},"string",[61,741,742,748],{},[82,743,744,115,746],{},[25,745,258],{},[25,747,261],{},[82,749,750],{},"number",[61,752,753,760],{},[82,754,755,757,758],{},[25,756,358],{}," \u002F ",[25,759,366],{},[82,761,762,757,765],{},[25,763,764],{},"true",[25,766,767],{},"false",[61,769,770,774],{},[82,771,772],{},[25,773,267],{},[82,775,776],{},[25,777,778],{},"null",[15,780,781],{},"The edges:",[490,783,784,801,831,856,869],{},[219,785,786,789,790,793,794,797,798,800],{},[30,787,788],{},"Tuples become arrays."," ",[25,791,792],{},"json.dumps((1, 2))"," gives ",[25,795,796],{},"[1, 2]",", and reading it back gives a ",[25,799,253],{},", not a tuple. Tuples do not survive a round trip.",[219,802,803,789,806,793,809,812,813,816,817,115,819,115,821,115,823,119,825,827,828,227],{},[30,804,805],{},"Non-string keys become strings.",[25,807,808],{},"json.dumps({1: \"a\"})",[25,810,811],{},"{\"1\": \"a\"}",", and the key comes back as ",[25,814,815],{},"\"1\"",". Keys must be ",[25,818,114],{},[25,820,258],{},[25,822,261],{},[25,824,264],{},[25,826,267],{},"; a tuple key raises ",[25,829,830],{},"TypeError: keys must be str, int, float, bool or None, not tuple",[219,832,833,842,843,115,845,847,848,851,852,855],{},[30,834,835,169,838,841],{},[25,836,837],{},"NaN",[25,839,840],{},"Infinity"," are not JSON."," Python writes them by default (",[25,844,837],{},[25,846,840],{},"), but strict parsers reject that output. Pass ",[25,849,850],{},"allow_nan=False"," to raise ",[25,853,854],{},"ValueError: Out of range float values are not JSON compliant"," instead of emitting invalid JSON.",[219,857,858,789,861,864,865,868],{},[30,859,860],{},"Duplicate keys: last one wins.",[25,862,863],{},"json.loads('{\"a\": 1, \"a\": 2}')"," returns ",[25,866,867],{},"{\"a\": 2}",". There is no warning, so a typo in a hand-written file can silently drop data.",[219,870,871,874,875,115,878,881,882,885,886,227],{},[30,872,873],{},"Sets, datetimes, and custom classes are not serializable"," by default. Convert them first (",[25,876,877],{},"list(s)",[25,879,880],{},"dt.isoformat()","), or pass ",[25,883,884],{},"default="," to ",[25,887,888],{},"json.dumps",[50,890,892],{"id":891},"validate-format-and-model-json-before-it-reaches-your-script","Validate, format, and model JSON before it reaches your script",[15,894,895,896,898],{},"Most time lost to JSON bugs in Python is spent debugging data that was already broken. Three browser tools cover the steps before ",[25,897,188],{}," runs:",[490,900,901,910,919],{},[219,902,903,909],{},[30,904,905],{},[19,906,908],{"href":907},"\u002Ftools\u002Fformat\u002Fjson-editor","JSON Editor"," — paste or open the file to validate it live, format it with two or four spaces, minify it, and copy or download the result. Useful for confirming a file is valid before you point a script at it, and for pretty-printing an API response you are about to paste into a test fixture.",[219,911,912,918],{},[30,913,914],{},[19,915,917],{"href":916},"\u002Ftools\u002Fconvert\u002Fjson-array-generator","JSON Array Generator"," — when you need sample data rather than a single object: set the item count and field names and download a pretty-printed JSON array to use as test input.",[219,920,921,927,928,931,932,935,936,939],{},[30,922,923],{},[19,924,926],{"href":925},"\u002Ftools\u002Fconvert\u002Fjson-to-code","JSON to Code Generator"," — paste a representative sample and choose Python to get a starter ",[25,929,930],{},"dataclass"," (arrays become ",[25,933,934],{},"list[Item]",", nested objects become separate declarations), then copy or download the ",[25,937,938],{},".py"," file. The output is inferred from the sample, so review naming, optional fields, and nullability — it describes the shape, it does not validate runtime data.",[15,941,942],{},[943,944],"img",{"alt":945,"src":946},"JSON Editor tool interface","\u002Fblog\u002Fscreenshots\u002Fen\u002Fread-json-file-python\u002F01-json-editor.webp",[15,948,949],{},[645,950,951],{},"The JSON Editor tool.",[15,953,954],{},"All three run in your browser and do not upload the data, which matters when the JSON is a production payload.",[50,956,958],{"id":957},"faq","FAQ",[960,961,963],"h3",{"id":962},"what-is-the-difference-between-jsonload-and-jsonloads","What is the difference between json.load() and json.loads()?",[15,965,966,968,969,972,973,975,976,978,979,115,981,983],{},[25,967,188],{}," takes a file-like object and reads from it; ",[25,970,971],{},"json.loads()"," takes a string and parses it. The ",[25,974,34],{}," stands for string. Use ",[25,977,179],{}," with ",[25,980,240],{},[25,982,168],{}," with text you already have.",[960,985,987],{"id":986},"how-do-i-read-a-json-file-in-python","How do I read a JSON file in Python?",[37,989,992],{"className":990,"code":991,"language":42,"meta":43},[40],"import json\n\nwith open(\"data.json\", \"r\", encoding=\"utf-8\") as f:\n    data = json.load(f)\n",[25,993,991],{"__ignoreMap":43},[15,995,996,997,1000],{},"Add ",[25,998,999],{},"encoding=\"utf-8-sig\""," if the file may have a BOM.",[960,1002,1004],{"id":1003},"how-do-i-write-json-to-a-file-in-python","How do I write JSON to a file in Python?",[37,1006,1009],{"className":1007,"code":1008,"language":42,"meta":43},[40],"with open(\"data.json\", \"w\", encoding=\"utf-8\") as f:\n    json.dump(data, f, indent=2, ensure_ascii=False)\n",[25,1010,1008],{"__ignoreMap":43},[960,1012,1014],{"id":1013},"what-is-the-difference-between-jsondump-and-jsondumps","What is the difference between json.dump() and json.dumps()?",[15,1016,1017,1020,1021,1024,1025,1027,1028,757,1030,227],{},[25,1018,1019],{},"json.dump()"," writes to a file object; ",[25,1022,1023],{},"json.dumps()"," returns a string. Same ",[25,1026,34],{}," rule as ",[25,1029,179],{},[25,1031,168],{},[960,1033,1035],{"id":1034},"how-do-i-pretty-print-json-in-python","How do I pretty-print JSON in Python?",[15,1037,1038,1039,1041,1042,1045,1046,1048,1049,227],{},"Pass ",[25,1040,345],{},": ",[25,1043,1044],{},"json.dumps(data, indent=2)",". For a file, ",[25,1047,401],{},". From the shell, ",[25,1050,1051],{},"python -m json.tool data.json",[960,1053,1055],{"id":1054},"why-do-i-get-unexpected-utf-8-bom","Why do I get \"Unexpected UTF-8 BOM\"?",[15,1057,1058,1059,1061],{},"The file starts with a byte-order mark, usually because it was saved by a Windows editor. Open it with ",[25,1060,999],{},", which strips the BOM.",[960,1063,1065],{"id":1064},"which-exceptions-should-i-catch-when-reading-json","Which exceptions should I catch when reading JSON?",[15,1067,1068,169,1070,1072,1073,1075,1076,1078,1079,602,1081,1083,1084,1086],{},[25,1069,548],{},[25,1071,558],{}," for the file itself, ",[25,1074,568],{}," for invalid content, and ",[25,1077,578],{}," for encoding mismatches. ",[25,1080,601],{},[25,1082,605],{},", so a single ",[25,1085,609],{}," covers it.",[960,1088,1090],{"id":1089},"how-do-i-handle-large-json-files-in-python","How do I handle large JSON files in Python?",[15,1092,1093,1094,1097,1098,1100,1101,1103],{},"For JSON Lines, iterate the file and call ",[25,1095,1096],{},"json.loads(line)"," per line. For one huge document, use an incremental parser such as ",[25,1099,687],{},". ",[25,1102,188],{}," always loads the entire document into memory.",[960,1105,1107],{"id":1106},"is-a-python-dict-the-same-as-a-json-object","Is a Python dict the same as a JSON object?",[15,1109,1110,1111,1113],{},"No. A dict is an in-memory Python structure whose keys can be any hashable type; a JSON object is text with string keys. Serialization converts between them, and the conversion is lossy in places: tuples become arrays, non-string keys become strings, and ",[25,1112,837],{}," has no valid JSON representation.",[50,1115,1117],{"id":1116},"whats-next","What's Next?",[490,1119,1120,1127,1135,1143,1151],{},[219,1121,1122,789,1125,227],{},[30,1123,1124],{},"Check a file before parsing it:",[19,1126,908],{"href":907},[219,1128,1129,1132,1133,227],{},[30,1130,1131],{},"Need sample data?"," Generate an array with the ",[19,1134,917],{"href":916},[219,1136,1137,1140,1141,227],{},[30,1138,1139],{},"Want a model for the payload?"," Generate a Python dataclass with the ",[19,1142,926],{"href":925},[219,1144,1145,1148,1149,227],{},[30,1146,1147],{},"Working with line-delimited data?"," Read ",[19,1150,680],{"href":679},[219,1152,1153,1156,1157,227],{},[30,1154,1155],{},"Debugging a parse failure?"," See ",[19,1158,652],{"href":651},[1160,1161],"hr",{},[15,1163,1164],{},[645,1165,1166],{},"All tools on JSON Toolbox run entirely in your browser. Your data never leaves your device.",{"title":43,"searchDepth":1168,"depth":1168,"links":1169},2,[1170,1171,1172,1173,1174,1175,1176,1177,1178,1179,1191],{"id":52,"depth":1168,"text":53},{"id":204,"depth":1168,"text":205},{"id":270,"depth":1168,"text":271},{"id":385,"depth":1168,"text":386},{"id":440,"depth":1168,"text":441},{"id":517,"depth":1168,"text":518},{"id":655,"depth":1168,"text":656},{"id":691,"depth":1168,"text":692},{"id":891,"depth":1168,"text":892},{"id":957,"depth":1168,"text":958,"children":1180},[1181,1183,1184,1185,1186,1187,1188,1189,1190],{"id":962,"depth":1182,"text":963},3,{"id":986,"depth":1182,"text":987},{"id":1003,"depth":1182,"text":1004},{"id":1013,"depth":1182,"text":1014},{"id":1034,"depth":1182,"text":1035},{"id":1054,"depth":1182,"text":1055},{"id":1064,"depth":1182,"text":1065},{"id":1089,"depth":1182,"text":1090},{"id":1106,"depth":1182,"text":1107},{"id":1116,"depth":1168,"text":1117},"json_tools","2026-09-29T00:00:00.000Z","Read and write JSON in Python: json.load vs json.loads, json.dump vs json.dumps, UTF-8 and BOM handling, exception handling, and streaming large files.",false,"md","\u002Fblog\u002Fcover\u002Fen\u002Fread-json-file-python-cover.svg",[1199],"en",{},true,"\u002Fen\u002Fblog\u002Fread-json-file-python",{"slug":1204,"text":1205,"btn":1206},"json-editor","Validate or format the JSON before it reaches your script:","Open JSON Editor",{"title":5,"description":1194},"en\u002Fblog\u002Fread-json-file-python",[707,704,1210,1211,1212],"json.load","Tutorial","Data Processing","bKVBn8yH9mc13rLKetkM2GOCNuD5dWhRcnZR_xSmaiA",[1215,1218],{"path":1216,"title":1217},"\u002Fen\u002Fblog\u002Fjson-vs-yaml","JSON vs YAML: Key Differences and When to Use Each",{"path":1219,"title":1220},"\u002Fen\u002Fblog\u002Ffree-realistic-json-test-data","Free Realistic JSON Test Data for Development and Performance Testing",1796356311652]