TIL51 - what is difference json and dictionary?

what is difference json and dictionary?


1. 궁금증

단위 테스트를 하다보면, POST, PATCH, PUT, DELETE를 할 때

입력할 데이터를 입력하고 json.dumps를 합니다.

회원가입 단위테스트를 예로 하면,

client = Client()

user_info = {
    "email"    : "[email protected]",
    "password" : "pass1234" 
}

response = client.post("/users/signup", json.dumps(user_info), content_type="application/json")

이렇게 됩니다.

json과 dictionary 모두 KEY, VALUE인데
왜 굳이 json 일까? 에서 두 개의 차이가 무엇일지라는 고민이 시작되었습니다.


2. 해결

가장 먼저 한 건 구글링이었습니다.

what is the difference between dictionary and json

구글링을 통해 얻은 차이점은 이렇습니다

  1. json은 Data Format이며, 순수 문자열들이어서 dictionary나 그외 형태로 해석 가능
    dictionary는 Data Structure임
  2. json의 KEY는 항상 문자열이여야 함
    dictionary의 KEY는 특정 데이터 타입에 구애 안받음
  3. json의 각 KEY는 undefined라는 기본값을 가짐
    dictionary는 기본값을 가지지 않음
  4. json은 true, false, null
    dictionary는 True, False, None

그 외 속도 관련한 것도 있는데, 제 수준에선 이정도로 차이가 있구나..하고 끝냈습니다.

다음 TIL포스팅은 기업과제에 대한 게 아닌 이상

the difference of json.loads and json.dump and json.dumps

입니다.

좋은 웹페이지 즐겨찾기