개요
ConvertTo-Json/ConvertFrom-Json은 PowerShell 객체와 JSON(JavaScript Object Notation) 문자열을 상호 변환하는 cmdlet 쌍이다. 27장에서 만든 [PSCustomObject]나 44장의 해시테이블을 파일로 저장하거나 웹 API로 주고받으려면, 결국 이 두 cmdlet을 거쳐 텍스트 형태로 직렬화(serialize)·역직렬화(deserialize)해야 한다.
정신 모델은 “ConvertTo-Json은 메모리 안의 객체 트리를 사람도 읽을 수 있는 텍스트로 평평하게 펼치고, ConvertFrom-Json은 그 텍스트를 다시 객체 트리로 복원한다"는 것이다. 이 왕복 변환이 정확히 대칭이 되려면 뒤에서 설명할 -Depth, -AsHashtable 같은 매개변수를 신경 써야 한다.
사용법
| |
매개변수
| 매개변수 | 대상 | 설명 |
|---|---|---|
-Depth | 둘 다 | 중첩 객체를 몇 단계까지 직렬화·역직렬화할지(ConvertTo-Json 기본값 2, ConvertFrom-Json 기본값 1024) |
-Compress | ConvertTo-Json | 들여쓰기·줄바꿈 없이 한 줄로 압축 출력 |
-AsArray | ConvertTo-Json | 요소가 하나뿐이어도 항상 JSON 배열([...])로 감싸기 |
-EnumsAsStrings | ConvertTo-Json | 열거형(enum) 값을 숫자가 아니라 이름 문자열로 출력 |
-AsHashtable | ConvertFrom-Json | PSCustomObject 대신 해시테이블(44장)로 반환, 대소문자만 다른 중복 키 등 예외 상황 회피 |
-NoEnumerate | ConvertFrom-Json | 배열을 파이프라인에 낱개로 풀어내지 않고 하나의 배열 객체로 유지 |
예시
| |
주의사항·함정
-Depth의 기본값이 얕아서, 3단계 이상 중첩된 객체는 조용히 잘린다: ConvertTo-Json의 -Depth 기본값은 2다. 해시테이블 안에 해시테이블을 여러 겹 넣은 복잡한 설정 구조를 그대로 직렬화하면, 일부 하위 수준이 "System.Collections.Hashtable" 같은 타입 이름 문자열로 뭉개져 출력된다. 오류 없이 조용히 정보가 손실되므로, 중첩 구조를 다룰 때는 항상 -Depth를 필요한 만큼 명시적으로 늘려야 한다.
ConvertFrom-Json의 기본 출력은 파이프라인으로 배열을 풀어 흘려보낸다: JSON 배열이 요소를 하나만 담고 있으면, 기본 동작상 ConvertFrom-Json | ConvertTo-Json으로 왕복했을 때 배열이 아니라 단일 값으로 바뀌어 버린다. 배열 구조 자체를 정확히 보존해야 하는 라운드트립 작업이라면 -NoEnumerate가 필요하다.
대소문자만 다른 키가 있는 JSON은 기본 변환에서 예외가 날 수 있다: PSCustomObject는 속성 이름의 대소문자를 구분하지 않으므로, {"key":"a", "Key":"b"}처럼 대소문자만 다른 두 키가 있는 JSON은 기본 ConvertFrom-Json에서 오류를 일으키거나 마지막 키만 남는다. 이런 데이터를 다뤄야 한다면 -AsHashtable을 쓰는 것이 안전하다.
이식성: Bash/CMD에는 JSON을 다루는 내장 문법이 없어 jq 같은 외부 도구에 의존해야 한다. PowerShell은 객체 파이프라인(10장)의 자연스러운 연장으로 JSON 직렬화가 코어 cmdlet에 내장돼 있다는 점이 근본적으로 다르다 — 이는 Invoke-RestMethod(16부)가 웹 API 응답을 자동으로 객체로 파싱해 주는 것과 같은 설계 철학이다.
![Featured image of post [PowerShell] 48. ConvertTo-Json/ConvertFrom-Json](/post/powershell/convertto-convertfrom-json-powershell/wordcloud_hu_c8566c139d93f21d.webp)
![[PowerShell] 46. Select-String — 텍스트에서 패턴 찾기](/post/powershell/select-string-command-powershell/wordcloud_hu_76a0521c6b7afac7.webp)
![[PowerShell] 47. -match/-replace 연산자와 정규식](/post/powershell/match-replace-regex-powershell/wordcloud_hu_c39cd98b9fc6220f.webp)
![[PowerShell] 48. ConvertTo-Json/ConvertFrom-Json](/post/powershell/convertto-convertfrom-json-powershell/wordcloud_hu_a8973bead7a830f9.webp)
![[PowerShell] 49. Import-Csv/Export-Csv/ConvertTo-Csv](/post/powershell/import-export-csv-command-powershell/wordcloud_hu_41747e6dfda6f23e.webp)
![[PowerShell] 50. ConvertTo-Html과 Out-File](/post/powershell/convertto-html-out-file-powershell/wordcloud_hu_d068d41aa5ff8785.webp)
![[PowerShell] 113. Invoke-WebRequest/Invoke-RestMethod](/post/powershell/invoke-webrequest-invoke-restmethod-powershell/wordcloud_hu_d83d75f2879b9315.webp)
![[PowerShell] 41. 변수와 데이터 타입](/post/powershell/variables-data-types-powershell/wordcloud_hu_6051f7a36d4700ee.webp)
![[PowerShell] 42. 배열과 컬렉션 기초](/post/powershell/arrays-collections-powershell/wordcloud_hu_c295cc73d4592ae5.webp)
![[PowerShell] 43. ArrayList와 Generic List(List<T>)](/post/powershell/arraylist-generic-list-powershell/wordcloud_hu_a34550a6370cee87.webp)
![[PowerShell] 44. 해시테이블(Hashtable) 다루기](/post/powershell/hash-tables-powershell/wordcloud_hu_d1fd29269ce3c32d.webp)