EDXUnescapeJSON

Learn about the EDXUnescapeJSON Edge Delta OTTL extension function.

Minimum Agent Version: v1.31.0

Overview

EDXUnescapeJSON fills a gap in standard OTTL’s JSON handling capabilities. While OTTL provides ParseJSON for parsing JSON strings, it cannot handle multiply-escaped JSON strings that are common in nested logging scenarios. This Edge Delta extension recursively unescapes JSON strings, making it possible to properly parse deeply nested or repeatedly escaped JSON data.

Syntax

EDXUnescapeJSON(input)
  • input: The escaped JSON string to unescape.

Examples

Input

{
  "_type": "log",
  "timestamp": 1735789800000,
  "body": "Processing escaped JSON data",
  "resource": {...},
  "attributes": {
    "escaped_json": "{\\\"level\\\":\\\"info\\\",\\\"message\\\":\\\"User logged in\\\",\\\"metadata\\\":\\\"{\\\\\\\"user_id\\\\\\\":\\\\\\\"12345\\\\\\\",\\\\\\\"session\\\\\\\":\\\\\\\"abc-xyz\\\\\\\"}\\\"}"
  },
  "cache": {
    "body": "{\\\"level\\\":\\\"info\\\",\\\"message\\\":\\\"User logged in\\\",\\\"metadata\\\":\\\"{\\\\\\\"user_id\\\\\\\":\\\\\\\"12345\\\\\\\",\\\\\\\"session\\\\\\\":\\\\\\\"abc-xyz\\\\\\\"}\\\"}"
  }
}

Statement

set(attributes["unescaped_json"], EDXUnescapeJSON(cache["body"]))
set(attributes["parsed_data"], ParseJSON(attributes["unescaped_json"]))

Output

{
  "_type": "log",
  "timestamp": 1735789830000,
  "body": "Processing escaped JSON data",
  "resource": {...},
  "attributes": {
    "escaped_json": "{\\\"level\\\":\\\"info\\\",\\\"message\\\":\\\"User logged in\\\",\\\"metadata\\\":\\\"{\\\\\\\"user_id\\\\\\\":\\\\\\\"12345\\\\\\\",\\\\\\\"session\\\\\\\":\\\\\\\"abc-xyz\\\\\\\"}\\\"}",
    "unescaped_json": "{\"level\":\"info\",\"message\":\"User logged in\",\"metadata\":\"{\\\"user_id\\\":\\\"12345\\\",\\\"session\\\":\\\"abc-xyz\\\"}\"}",
    "parsed_data": {
      "level": "info",
      "message": "User logged in",
      "metadata": "{\"user_id\":\"12345\",\"session\":\"abc-xyz\"}"
    }
  },
  "cache": {
    "body": "{\\\"level\\\":\\\"info\\\",\\\"message\\\":\\\"User logged in\\\",\\\"metadata\\\":\\\"{\\\\\\\"user_id\\\\\\\":\\\\\\\"12345\\\\\\\",\\\\\\\"session\\\\\\\":\\\\\\\"abc-xyz\\\\\\\"}\\\"}"
  }
}

The EDXUnescapeJSON function has recursively unescaped the JSON string, making it possible to properly parse the nested JSON structure.