Server-Side Script/Python

json & working with json in django

unkwn98 2021. 6. 24. 11:35

what is JSON? & let's see how to handle JSON data in Django using python

 

JSON: JavaScript Object Notation => a standard text-based data format for representing structured data

   - commonly used for transmitting data in web applications (server <=> client)

   - json exists as a string => needs to be converted to a native JavaScript object when assessing the data

   - json is just a data format + 어떠한 통신 방법, 프로그래밍 문법도 아닌 단순히 데이터를 표시하는 표현 방법이다.

   - json 형식은 key / value가 존재할 수 있으며 key값이나 문자열은 항상 "" 표기한다.

{
  "squadName": "Super hero squad",
  "homeTown": "Metro City",
  "formed": 2016,
  "secretBase": "Super tower",
  "active": true,
  "members": [
    {
      "name": "Molecule Man",
      "age": 29,
      "secretIdentity": "Dan Jukes",
      "powers": [
        "Radiation resistance",
        "Turning tiny",
        "Radiation blast"
      ]
    },
    {
      "name": "Madame Uppercut",
      "age": 39,
      "secretIdentity": "Jane Wilson",
      "powers": [
        "Million tonne punch",
        "Damage resistance",
        "Superhuman reflexes"
      ]
    },
    {
      "name": "Eternal Flame",
      "age": 1000000,
      "secretIdentity": "Unknown",
      "powers": [
        "Immortality",
        "Heat Immunity",
        "Inferno",
        "Teleportation",
        "Interdimensional travel"
      ]
    }
  ]
}

🥜 python으로 json data 가져오기

   - json.loads() takes in a string and returns a json object

   - json.dumps() takes in a json object and returns a string

reference: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON