파이썬을 기반으로 개발 시, 많이 사용하는 내장모듈 중 하나인 json
에 대해 알아보자
아래 많이 사용하는 대표적 네 가지의 함수가 있다.
JSON 문자열
을python 객체
로 변환
python 객체
를JSON 문자열
로 변환
JSON 파일
을python 객체
로 불러옴
python 객체
를JSON 파일
에 저장
디폴트로 변환되는 JSON 문자열은 위와 같이 한 줄이기 때문에 Python 객체가 많은 데이터를 담고 있는 경우, 가독성이 떨어질 수 있습니다. 이럴 경우,
indent
파라미터에 숫자를 넘기면 그 만큼 들여쓰기가 되어 JSON 문자열로 변환이 됩니다.
ex)
mport json
json_object = {"id": 1,
"username": "John",
"email": "John@google.com",}
json_string = json.dumps(json_object, indent=2)
print(json_string)
indent 없는 output
{"id": 1,"username": "John","email": "John@google.com"}
indent=2
output
{
"id": 1,
"username": "John",
"email": "John@google.com"
}