> For the complete documentation index, see [llms.txt](https://batiai.gitbook.io/service/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://batiai.gitbook.io/service/action/guide_json_and_xml.md).

# 데이터 추출 가이드: JSON & XML

### 데이터 추출 가이드: JSON & XML

이 문서는 복잡해 보이는 데이터 묶음인 JSON과 XML에서, 사용자가 원하는 정보만 정확하고 쉽게 골라내는 방법을 안내합니다.

먼저 아래의 **핵심 문법 요약**을 통해 전체적인 사용법을 빠르게 파악할 수 있습니다. 각 문법에 대한 자세한 설명과 활용 사례는 문서 하단의 상세 가이드에서 단계별로 학습할 수 있습니다.

***

#### **1. 핵심 문법 요약 (Cheat Sheet)**

바쁜 사용자를 위해 JSONPath와 XML 추출 문법의 핵심 기능만 모아두었습니다.

**1) JSONPath 핵심 문법**

| 기호         | 기능                                              | 대표 사용 예시        |
| ---------- | ----------------------------------------------- | --------------- |
| **`$`**    | **루트(Root)**: 데이터 전체의 시작점을 의미합니다.               | `$`             |
| **`.`**    | **하위 접근**: 특정 항목 내부의 값에 접근합니다.                  | `$.user.name`   |
| **`[*]`**  | **배열 전체(Wildcard)**: 목록(배열)의 모든 요소를 선택합니다.      | `$.products[*]` |
| **`[숫자]`** | **배열 인덱스**: 목록(배열)에서 특정 순서의 요소를 선택합니다. (0부터 시작) | `$.products[0]` |

**2) XML 추출 핵심 문법**

| 문법         | 기능                                                         | 대표 사용 예시                |
| ---------- | ---------------------------------------------------------- | ----------------------- |
| `element`  | **요소 선택**: 추출할 데이터가 담긴 XML 요소(Element)의 이름을 지정합니다.         | `item`                  |
| `\|`       | **컬럼 지정**: 선택한 요소 내부에서 가져올 하위 요소들을 지정합니다.                  | `item \| title, author` |
| `as`       | **별칭(Alias) 지정**: 추출한 데이터의 컬럼(표의 제목) 이름을 변경합니다.            | `title as 제목`           |
| `;`        | **옵션 구분**: 여러 옵션(조건, 정렬 등)을 연결할 때 사용합니다.                   | `... ; limit 10`        |
| `where`    | **조건 필터링**: 특정 조건을 만족하는 데이터만 필터링합니다.                       | `where brand=Apple`     |
| `order by` | **데이터 정렬**: 지정한 기준에 따라 데이터를 정렬합니다. (asc: 오름차순, desc: 내림차순) | `order by price desc`   |
| `limit`    | **개수 제한**: 가져올 데이터의 최대 개수를 제한합니다.                          | `limit 5`               |

***

#### **2. 상세 가이드: JSON 데이터 추출 (JSONPath)**

JSON은 `{ "키": "값" }` 형태로 데이터를 정리하는 방식입니다. 마치 각 정보에 '이름표'가 붙어있는 서랍장과 같습니다. JSONPath는 이 서랍장에서 원하는 물건을 찾기 위한 '주소' 역할을 합니다.

**1단계: 기본 항목 추출**

가장 기본적인 데이터 구조에서 특정 값을 가져옵니다.

* **예시 데이터:**

  ```json
  {
    "name": "홍길동",
    "age": 25,
    "city": "서울"
  }
  ```
* **`$.name`** → 결과: `"홍길동"`
* **`$.age`** → 결과: `25`

**2단계: 중첩된 객체 항목 추출**

객체 안에 또 다른 객체가 있는 중첩된 구조에서 값을 가져옵니다. `.`을 사용하여 단계별로 접근합니다.

* **예시 데이터:**

  ```json
  {
    "user": {
      "profile": {
        "name": "김철수",
        "email": "kim@example.com"
      }
    }
  }
  ```
* **`$.user.profile.name`** → 결과: `"김철수"`
* **해설**: `user` 객체 안의 `profile` 객체 안의 `name` 값을 의미합니다.

**3단계: 배열(목록) 데이터 다루기**

`[]` 기호로 묶인 데이터 목록을 배열이라고 합니다. 배열 전체를 가져오거나, 특정 순서의 값만 가져올 수 있습니다.

* **예시 데이터:**

  ```json
  {
    "fruits": ["사과", "바나나", "오렌지"]
  }
  ```
* **배열 전체 가져오기**: `$.fruits[*]` → 결과: `["사과", "바나나", "오렌지"]`
* **특정 순서의 값 가져오기**: 컴퓨터는 숫자를 0부터 셉니다.
  * `$.fruits[0]` → 결과: `"사과"` (첫 번째 항목)
  * `$.fruits[2]` → 결과: `"오렌지"` (세 번째 항목)

**4단계: 객체 배열에서 특정 값 추출하기**

객체(서랍 칸)들이 배열(목록) 형태로 여러 개 있을 때, 모든 객체에서 공통된 항목의 값들만 추출할 수 있습니다.

* **예시 데이터:**

  ````json
  {
    "users": [
      { "id": 1, "name": "홍길동" },
      { "id": 2, "name": "김영희" }
    ]
  }
  ```*   **모든 사용자의 이름 추출**: `$.users[*].name` → 결과: `["홍길동", "김영희"]`
  ````
* **해설**: `users` 배열의 모든(`*`) 항목에 접근하여, 각 항목의 `name` 값을 가져오라는 의미입니다.

***

#### **3. 상세 가이드: XML 데이터 추출**

XML은 `<이름표>내용</이름표>` 형태로 데이터의 시작과 끝을 감싸서 구조를 표현하는 방식입니다. 이 이름표(Element)를 기준으로 원하는 정보를 추출하여 표 형태로 정리할 수 있습니다.

**1단계: 기본 요소 및 컬럼 선택**

가장 기본적으로 특정 요소(`element`)를 선택하고, 그 안에서 원하는 하위 요소(`column`)들을 가져옵니다.

* **예시 데이터:**

  ```xml
  <products>
    <product>
      <name>무선 이어폰</name>
      <price>89000</price>
    </product>
    <product>
      <name>블루투스 스피커</name>
      <price>156000</price>
    </product>
  </products>
  ```
* **`product | name, price`**: `product` 요소 안의 `name`과 `price`를 추출합니다.

**2단계: 컬럼명 변경하기 (as)**

추출한 데이터 표의 제목(컬럼명)을 더 이해하기 쉬운 이름으로 변경할 수 있습니다.

* **`product | name as 상품명, price as 가격`**: `name`은 '상품명'으로, `price`는 '가격'으로 컬럼명이 변경되어 표시됩니다.

**3단계: 조건부 필터링 (where)**

원하는 조건에 맞는 데이터만 골라서 가져올 수 있습니다.

* **예시 데이터:**

  ```xml
  <products>
    <product>
      <name>iPhone 15</name>
      <brand>Apple</brand>
    </product>
    <product>
      <name>Galaxy S24</name>
      <brand>Samsung</brand>
    </product>
  </products>
  ```
* **`product | name; where brand=Apple`**: `brand`가 'Apple'인 `product`의 `name`만 추출합니다. → 결과: `iPhone 15`

**4단계: 데이터 정렬 및 개수 제한 (order by, limit)**

추출한 결과를 특정 기준에 따라 정렬하거나, 가져올 개수를 제한할 수 있습니다.

* **예시:** 발행일(pubDate)을 기준으로 최신순(내림차순, `desc`)으로 정렬하여 상위 5개만 가져오고 싶을 때
* **`item | title, pubDate; order by pubDate desc; limit 5`**
* **해설**:
  1. `item` 요소에서 `title`과 `pubDate`를 선택합니다.
  2. `pubDate`를 기준으로 내림차순(`desc`) 정렬합니다.
  3. 정렬된 결과 중 위에서부터 5개(`limit 5`)만 최종 결과로 보여줍니다.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://batiai.gitbook.io/service/action/guide_json_and_xml.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
