본문 바로가기
다양한 TIP

한줄 json 파일 깔끔하게 보는 방법(pretty)

by 유기농프로그래밍 2023. 8. 9.
반응형

Json Viewing

설정파일들을 만지다보면, json 형태로 된 txt를 읽게 되는 경우가 있습니다.

이러한 경우 json 포멧이 맞춰지지 않았거나 용량을 줄이기 위해 한줄로 정리되어 있다면 읽기 어렵겠죠?

이 읽기 어려운걸 읽기 쉽게 하는 방법을 공유하도록 하겠습니다.

 


 

not_pretty.json 을 아래와 같이 만들어보겠습니다.

{"novelTitle": "The Adventure Chronicles","author": {"name": "Jane Johnson","birthYear": 1985,"nationality": "American"},"characters": [{"name": "Eleanor","age": 28,"occupation": "Archaeologist","traits": ["adventurous", "curious", "resourceful"],"relationships": {"partner": "Nathan","rival": "Victoria"}},{"name": "Nathan","age": 32,"occupation": "Historian","traits": ["analytical", "knowledgeable", "supportive"],"relationships": {"partner": "Eleanor","friend": "Marcus"}},{"name": "Victoria","age": 26,"occupation": "Treasure Hunter","traits": ["cunning", "ambitious", "mysterious"],"relationships": {"rival": "Eleanor"}}],"setting": {"location": "Lost Temples of Eldoria","timePeriod": "1920s","worldType": "Fantasy"},"plot": {"summary": "Eleanor, Nathan, and Victoria embark on a perilous journey to uncover the secrets of the Lost Temples of Eldoria. Rivalries and alliances form as they race to find a legendary artifact."}}

 

이제 명령어 python3 -m json.tool 을 이용하여 출력해보겠습니다.

python3 -m json.tool not_pretty.json
{
    "novelTitle": "The Adventure Chronicles",
    "author": {
        "name": "Jane Johnson",
        "birthYear": 1985,
        "nationality": "American"
    },
    "characters": [
        {
            "name": "Eleanor",
            "age": 28,
            "occupation": "Archaeologist",
            "traits": [
                "adventurous",
                "curious",
                "resourceful"
            ],
            "relationships": {
                "partner": "Nathan",
                "rival": "Victoria"
            }
        },
        {
            "name": "Nathan",
            "age": 32,
            "occupation": "Historian",
            "traits": [
                "analytical",
                "knowledgeable",
                "supportive"
            ],
            "relationships": {
                "partner": "Eleanor",
                "friend": "Marcus"
            }
        },
        {
            "name": "Victoria",
            "age": 26,
            "occupation": "Treasure Hunter",
            "traits": [
                "cunning",
                "ambitious",
                "mysterious"
            ],
            "relationships": {
                "rival": "Eleanor"
            }
        }
    ],
    "setting": {
        "location": "Lost Temples of Eldoria",
        "timePeriod": "1920s",
        "worldType": "Fantasy"
    },
    "plot": {
        "summary": "Eleanor, Nathan, and Victoria embark on a perilous journey to uncover the secrets of the Lost Temples of Eldoria. Rivalries and alliances form as they race to find a legendary artifact."
    }
}

 

또 다른 방법으로는 text를 복사해서 출력할 수 있습니다.

 

json 문장을 ''로 감싼 후 echo로 출력, 그리고 파이프라인으로 명령어를 입력하는 거죠.

echo '{"novelTitle": "The Adventure Chronicles","author": {"name": "Jane Johnson","birthYear": 1985,"nationality": "American"},"characters": [{"name": "Eleanor","age": 28,"occupation": "Archaeologist","traits": ["adventurous", "curious", "resourceful"],"relationships": {"partner": "Nathan","rival": "Victoria"}},{"name": "Nathan","age": 32,"occupation": "Historian","traits": ["analytical", "knowledgeable", "supportive"],"relationships": {"partner": "Eleanor","friend": "Marcus"}},{"name": "Victoria","age": 26,"occupation": "Treasure Hunter","traits": ["cunning", "ambitious", "mysterious"],"relationships": {"rival": "Eleanor"}}],"setting": {"location": "Lost Temples of Eldoria","timePeriod": "1920s","worldType": "Fantasy"},"plot": {"summary": "Eleanor, Nathan, and Victoria embark on a perilous journey to uncover the secrets of the Lost Temples of Eldoria. Rivalries and alliances form as they race to find a legendary artifact."}}' | python3 -m json.tool

결과는 동일하게 출력되는걸 볼 수 있습니다.

 

유용하게 활용하세요~

반응형

댓글