{"type": "session", "harness": "deepseek-harness", "id": "misc-0017-p4", "name": "JSON parsing in Python part 4"} {"type": "message", "message": {"role": "system", "content": "You are a helpful Python tutor."}} {"type": "message", "message": {"role": "user", "content": "What is the fastest JSON library in Python (orjson/msgspec/cysimdjson) and when should I use each?"}} {"type": "message", "message": {"role": "assistant", "content": "Fastest: msgspec\n\nUsage:\n```\n#pip install msgspec\nimport msgspec\nclass User(msgspec.Struct):\n name: str\n age: int\njson_bytes = b'{\"name\": \"Alice\", \"age\": 30}' # Decodes and validates at the same time\nuser = msgspec.json.decode(json_bytes, type=User) \n```\nor\n```\n# pip install orjson\nimport orjson\n\n# Decoding (Parsing)\njson_bytes = b'{\"name\": \"Alice\", \"age\": 30}'\ndata = orjson.loads(json_bytes) # Note: Works best with bytes\n\n# Encoding\nserialized = orjson.dumps(data)\n```\nor\n```\n#pip install cysimdjson\nimport cysimdjson\n\njson_bytes = b'''\n{\n \"foo\": [1,2,[3]]\n}\n'''\n\nparser = cysimdjson.JSONParser()\njson_element = parser.parse(json_bytes)\n\n# Access using JSON Pointer\nprint(json_element.at_pointer(\"/foo/2/0\"))\n```\nor\n```\npip install yyjson\nimport yyjson\n\n# --- 1. Fast Decoding ---\njson_bytes = b'{\"project\": \"yyjson\", \"language\": \"C\", \"score\": 9.9}'\ndata = yyjson.loads(json_bytes)\nprint(data[\"project\"]) # yyjson\n\n# --- 2. Fast Encoding ---\nserialized = yyjson.dumps(data)\n```"}}