JSON

Updated:

JSON

  • JavaScript Object Notation
  • { key: value } 딕셔너리와 비슷한 형태

  • 데이터의 형식
  • 웹 환경에서 데이터를 주고 박는 가장 표준적인 방식
  • 키를 이용하여 원하는 데이터만 빠르게 추출 가능
  • 데이터가 쉽게 오염되지 않음
  • 다른 포맷에 비해 용량이 조금 큰 편

JSON과 딕셔너리 변환

  • in python

    * JSON -> loads() -> 딕셔너리
    * 딕셔너리 -> dumps() -> JSON
    
  • loads(): JSON 형태의 문자열을 딕셔너리로 변환합니다.
    이 때 딕셔너리의 모든 원소는 문자열 타입으로 설정됩니다.

# json을 dict로 바꾸어주는 함수
def create_dict(filename):
  with open(filename) as file:
    json_string = file.read()
    dicts = json.loads(json_string)
    
    return dicts
  • dumps(): 딕셔너리를 JSON 형태의 문자열로 변환합니다.
# dict를 json으로 바꾸로 파일에 써주는 함수
def create_json(dictionary, filename):
  with open(filename, 'w') as file:
      # 함수를 완성하세요.
      json_string = json.dumps(dictionary)
      file.write(json_string)

jsonify(JSON_TYPE_문자열)

  • JSON_TYPE의 문자열을 dict형태로 변경

Leave a comment