> ## Documentation Index
> Fetch the complete documentation index at: https://axiom.co/docs/llms.txt
> Use this file to discover all available pages before exploring further.

<AgentInstructions>

## Submitting Feedback

If you encounter incorrect, outdated, or confusing documentation on this page, submit feedback:

POST https://axiom.co/docs/feedback

```json
{
  "path": "/apl/scalar-functions/string-functions/parse-bytes",
  "feedback": "Description of the issue"
}
```

Only submit feedback when you have something specific and actionable to report.

</AgentInstructions>

# parse_bytes

> This page explains how to use the parse_bytes function in APL.

The `parse_bytes` function parses a string representation of data size (like `1 KB`, `500 MB`) and returns the numeric value in bytes. Use this function to convert human-readable byte strings from logs or configuration files into numeric values for calculations and comparisons.

## For users of other query languages

If you come from other query languages, this section explains how to adjust your existing queries to achieve the same results in APL.

<AccordionGroup>
  <Accordion title="Splunk SPL users">
    In Splunk SPL, you typically need custom eval expressions to parse byte strings. APL's `parse_bytes` provides this functionality natively.

    <CodeGroup>
      ```sql Splunk example theme={null}
      | eval bytes=case(
          match(size_str, "KB"), tonumber(replace(size_str, " KB", "")) * 1024,
          match(size_str, "MB"), tonumber(replace(size_str, " MB", "")) * 1048576,
          true(), 0)
      ```

      ```kusto APL equivalent theme={null}
      ['sample-http-logs']
      | extend bytes = parse_bytes(size_str)
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="ANSI SQL users">
    In ANSI SQL, parsing byte strings requires complex CASE statements. APL's `parse_bytes` simplifies this operation.

    <CodeGroup>
      ```sql SQL example theme={null}
      SELECT CASE 
        WHEN size_str LIKE '%KB%' THEN CAST(REPLACE(size_str, ' KB', '') AS FLOAT) * 1024
        WHEN size_str LIKE '%MB%' THEN CAST(REPLACE(size_str, ' MB', '') AS FLOAT) * 1048576
        ELSE 0
      END AS bytes FROM logs;
      ```

      ```kusto APL equivalent theme={null}
      ['sample-http-logs']
      | extend bytes = parse_bytes(size_str)
      ```
    </CodeGroup>
  </Accordion>
</AccordionGroup>

## Usage

### Syntax

```kusto theme={null}
parse_bytes(bytes_string, base)
```

### Parameters

| Name          | Type   | Required | Description                                                                           |
| ------------- | ------ | -------- | ------------------------------------------------------------------------------------- |
| bytes\_string | string | Yes      | A string representing a data size with units (for example, `1 KB`, `500 MB`, `2 GB`). |
| base          | int    | No       | Either 2 (default, 1024-based) or 10 (1000-based) for unit calculations.              |

### Returns

Returns the numeric value in bytes, or 0 if the string cannot be parsed.

## Use case examples

<Tabs>
  <Tab title="Log analysis">
    Parse human-readable size strings to analyze and aggregate data transfer volumes.

    **Query**

    ```kusto theme={null}
    ['sample-http-logs']
    | extend size_bytes = parse_bytes('512 KB')
    | where req_duration_ms > 3
    | summarize total_bytes = sum(size_bytes), request_count = count() by status
    | sort by total_bytes desc
    | limit 10
    ```

    [Run in Playground](https://play.axiom.co/axiom-play-qf1k/query?initForm=%7B%22apl%22%3A%22%5B%27sample-http-logs%27%5D%20%7C%20extend%20size_bytes%20%3D%20parse_bytes\(%27512%20KB%27\)%20%7C%20where%20req_duration_ms%20%3E%203%20%7C%20summarize%20total_bytes%20%3D%20sum\(size_bytes\)%2C%20request_count%20%3D%20count\(\)%20by%20status%20%7C%20sort%20by%20total_bytes%20desc%20%7C%20limit%2010%22%7D)

    **Output**

    | status | total\_bytes | request\_count |
    | ------ | ------------ | -------------- |
    | 200    | 4587520      | 8765           |
    | 500    | 1048576      | 2341           |
    | 404    | 524288       | 1234           |

    This query parses size strings to calculate total data transfer by HTTP status code, enabling volume-based analysis of API usage.
  </Tab>

  <Tab title="OpenTelemetry traces">
    Convert size strings from span attributes to numeric bytes for threshold-based analysis.

    **Query**

    ```kusto theme={null}
    ['otel-demo-traces']
    | extend payload_size = parse_bytes('256 KB', 2)
    | where duration > 100ms
    | summarize avg_size = avg(payload_size), span_count = count() by ['service.name']
    | sort by avg_size desc
    | limit 10
    ```

    [Run in Playground](https://play.axiom.co/axiom-play-qf1k/query?initForm=%7B%22apl%22%3A%22%5B%27otel-demo-traces%27%5D%20%7C%20extend%20payload_size%20%3D%20parse_bytes\(%27256%20KB%27%2C%202\)%20%7C%20where%20duration%20%3E%20100ms%20%7C%20summarize%20avg_size%20%3D%20avg\(payload_size\)%2C%20span_count%20%3D%20count\(\)%20by%20%5B%27service.name%27%5D%20%7C%20sort%20by%20avg_size%20desc%20%7C%20limit%2010%22%7D)

    **Output**

    | service.name | avg\_size | span\_count |
    | ------------ | --------- | ----------- |
    | checkout     | 262144    | 3421        |
    | frontend     | 262144    | 4532        |
    | cart         | 262144    | 2987        |

    This query converts size strings to bytes for numeric analysis of payload sizes across different services.
  </Tab>
</Tabs>

## List of related functions

* [format\_bytes](/apl/scalar-functions/string-functions/format-bytes): Formats numeric bytes as human-readable strings. Use this to reverse the parsing operation.
* [strlen](/apl/scalar-functions/string-functions/strlen): Returns the length of a string. Use this when you need string length rather than byte parsing.
