본문 바로가기

JSON3

Python으로 개행 문자(\n)가 포함된 JSON 읽기: JSONDecodeError 수정하기 Python json 모듈에서 개행 문자 "\r\n" 또는 " \n" 포함된 string을 json.loads() 함수로 dictionary로 객체로 변환할 때 발생하는 에러입니다. 개행 문자가 포함된 경우 json.loads() 함수 호출 시 "JSONDecodeError():Invalid control character" 에러가 발생하며 수정 방법은 ① json.loads() 호출 시 strict=True 값으로 전달하는 방법과 ② 원본 스트링에서 문구를 치환하는 방법 2가지가 있습니다. 에러 발생 예제 import json json_str = """{ "이름": "홍길동", "나이": 25, "문장": "첫 줄 문장 \n 두 번째 문장" }""" jdata_obj = json.loads(json_s.. 2021. 7. 14.
Dataframe(또는 Numpy)를 JSON 출력 시 에러: TypeError: Object of type int64 is not JSON serializable Pandas dataframe 또는 Numpy를 사용해서 JSON으로 출력하는 경우 발생하는 에러입니다. Pandas dataframe의 value list는 numpy를 사용하고 있고, Numpy의 data type은 Python에서 정의하고 있는 data type보다 많기 때문에 표준 Python data type만 처리하는 Json encoder에서 에러가 발생합니다. 이러한 문제점을 수정하기 위해서 custom json encoder를 정의하거나, numpyencoder를 설치해서 사용할 수 있습니다. Custome Json encoder 코드는 GitHub 링크를 확인해주세요. 에러 메시지: File "dataframe_to_json_test.py", line 22, in test print (.. 2021. 6. 20.
Python JSON 사용 시 TypeError: Object of type bytes is not JSON serializable Python 3.8 버전에서 Dictionary 데이터를 JSON변환 시 발생했던 TypeError: Object of type bytes is not JSON serializable 에러에 대한 수정 사항입니다. json.dumps()함수는 일반 obj를 JSON 포맷의 string으로 serialize 한다고 설명되어 있다(아래 그림) . 즉, Dictionary를 JSON으로 변환하기 위해서는 일반적으로 string으로 직렬화하여 전달해야 하지만, 직렬화가 정의되지 않은 byte array 로 전달하여 Type error 에러가 발생하는 것입니다. decode('utf8') 함수를 사용해서 byte array를 string으로 변환하여 수정할 수 있습니다. 수정 전 코드 def CreateMessa.. 2020. 6. 29.