파이썬 json 추출

조회수 454회
[
  {
    "type": "roster",
    "id": "407df467-f9f0-4745-8ed9-99f77cc25805",
    "attributes": {
      "stats": {
        "rank": 2,
        "teamId": 6
      },
      "won": "false",
      "shardId": "steam"
    },
    "relationships": {
      "team": {
        "data": null
      },
      "participants": {
        "data": [
          {
            "type": "participant",
            "id": "7b2cd1a7-f397-40a8-b4e9-1c1a57b74fd9"
          },
          {
            "type": "participant",
            "id": "aa324b12-0abf-4749-a0a1-008f22c3fa61"
          },
          {
            "type": "participant",
            "id": "bd685e98-c408-4497-b38a-4c51f20110d0"
          },
          {
            "type": "participant",
            "id": "5c12eeda-399a-4cc0-80af-a12034da6262"
          }
        ]
      }
    }
  },
  {
    "type": "roster",
    "id": "8da88c99-bf3b-4d9d-8387-f0290455786f",
    "attributes": {
      "won": "false",
      "shardId": "steam",
      "stats": {
        "rank": 15,
        "teamId": 2
      }
    },
    "relationships": {
      "team": {
        "data": null
      },
      "participants": {
        "data": [
          {
            "type": "participant",
            "id": "532aa009-6f68-4021-b267-b64bcbc2807e"
          },
          {
            "type": "participant",
            "id": "7bee10cc-25cf-45b9-ad70-ec897a892296"
          },
          {
            "type": "participant",
            "id": "cf903653-087a-4882-b649-383e178f554f"
          },
          {
            "type": "participant",
            "id": "ffa23ccc-8b13-460a-979a-f1b7ffbbb225"
          }
        ]
      }
    }
  },
  {
    "type": "roster",
    "id": "9393860c-f19c-4de3-9423-bcaaf598e7db",
    "attributes": {
      "shardId": "steam",
      "stats": {
        "rank": 3,
        "teamId": 15
      },
      "won": "false"
    },
    "relationships": {
      "participants": {
        "data": [
          {
            "type": "participant",
            "id": "26abe75e-bf59-4d70-8090-7dc534cf6a0b"
          },
          {
            "type": "participant",
            "id": "531c5730-1c38-44e6-b063-5d0097649bb3"
          },
          {
            "type": "participant",
            "id": "e404c991-31a9-4a84-9ece-2d7668ade425"
          },
          {
            "type": "participant",
            "id": "ff7d7bed-7446-499a-96e2-3f13f48afeed"
          }
        ]
      },
      "team": {
        "data": null
      }
    }
  },
  {
    "type": "roster",
    "id": "17427266-88bd-4af8-baaa-ae8a0dda0a08",
    "attributes": {
      "stats": {
        "rank": 13,
        "teamId": 14
      },
      "won": "false",
      "shardId": "steam"
    },
    "relationships": {
      "team": {
        "data": null
      },
      "participants": {
        "data": [
          {
            "type": "participant",
            "id": "9aeaf4f2-4ba5-432c-8fd5-c3b570e39c32"
          },
          {
            "type": "participant",
            "id": "157a185e-0b70-477c-86b2-5a04676d962e"
          },
          {
            "type": "participant",
            "id": "b686fb2b-1306-4062-b7a7-761942e7d32e"
          },
          {
            "type": "participant",
            "id": "1b6ec8f5-64db-4207-8091-e0ff01ef0815"
          }
        ]
      }
    }
  }
]

stats 부분들만 추출하고 싶은데 어떻게 해야할까요?

  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

  • 아주 기본적인 예입니다.

    >>> s = """..."""
    >>> import json
    >>> d = json.loads(s)
    >>> for e in d:
        print(e["attributes"]["stats"])
    
    
    {'rank': 2, 'teamId': 6}
    {'rank': 15, 'teamId': 2}
    {'rank': 3, 'teamId': 15}
    {'rank': 13, 'teamId': 14}
    >>> for e in d:
        print(e["id"], e["attributes"]["stats"])
    
    
    407df467-f9f0-4745-8ed9-99f77cc25805 {'rank': 2, 'teamId': 6}
    8da88c99-bf3b-4d9d-8387-f0290455786f {'rank': 15, 'teamId': 2}
    9393860c-f19c-4de3-9423-bcaaf598e7db {'rank': 3, 'teamId': 15}
    17427266-88bd-4af8-baaa-ae8a0dda0a08 {'rank': 13, 'teamId': 14}
    

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)