{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "hl7-json/1",
  "title": "HL7 v2 message as JSON (hl7-json/1)",
  "description": "Structural JSON for one HL7 v2 message. Positions are explicit, arrays preserve order, every value is the exact text from the message. No field names, data types or escape decoding. In style \"full\" every node carries \"value\" and \"empty\"; in style \"compact\" only leaf nodes carry \"value\".",
  "type": "object",
  "additionalProperties": false,
  "required": ["schema", "style", "message", "segments", "parseWarnings"],
  "properties": {
    "schema":        { "const": "hl7-json/1" },
    "style":         { "enum": ["full", "compact"] },
    "message":       { "$ref": "#/$defs/message" },
    "segments":      { "type": "array", "items": { "$ref": "#/$defs/segment" } },
    "parseWarnings": { "type": "array", "items": { "$ref": "#/$defs/warning" } }
  },
  "$defs": {
    "message": {
      "type": "object",
      "additionalProperties": false,
      "required": ["delimiters", "segmentCount", "lineEndings", "messageType", "controlId", "versionId"],
      "properties": {
        "delimiters": {
          "type": "object",
          "additionalProperties": false,
          "required": ["field", "component", "repetition", "escape", "subcomponent", "truncation"],
          "properties": {
            "field": { "type": "string" }, "component": { "type": "string" }, "repetition": { "type": "string" },
            "escape": { "type": "string" }, "subcomponent": { "type": "string" }, "truncation": { "type": ["string", "null"] }
          }
        },
        "segmentCount": { "type": "integer", "minimum": 1 },
        "lineEndings":  { "enum": ["CR", "LF", "CRLF", "mixed", "none"] },
        "messageType":  { "type": ["string", "null"], "description": "Raw text of MSH-9, or null if empty." },
        "controlId":    { "type": ["string", "null"], "description": "Raw text of MSH-10, or null if empty." },
        "versionId":    { "type": ["string", "null"], "description": "Raw text of MSH-12, or null if empty." }
      }
    },
    "segment": {
      "type": "object",
      "additionalProperties": false,
      "required": ["index", "id", "occurrence", "fieldCount", "fields"],
      "properties": {
        "index":      { "type": "integer", "minimum": 1, "description": "Position of the segment in the message (1-based)." },
        "id":         { "type": "string" },
        "occurrence": { "type": "integer", "minimum": 1, "description": "1-based count of segments with the same ID up to this one." },
        "fieldCount": { "type": "integer", "minimum": 0, "description": "Number of fields in the segment text, including empty and trailing empty ones." },
        "raw":        { "type": "string", "description": "Only when requested (includeRaw)." },
        "fields":     { "type": "array", "items": { "$ref": "#/$defs/field" } }
      }
    },
    "field": {
      "type": "object",
      "additionalProperties": false,
      "required": ["position", "path", "repetitions"],
      "properties": {
        "position":    { "type": "integer", "minimum": 1 },
        "path":        { "type": "string", "description": "Indexed path such as OBX[2]-5." },
        "special":     { "enum": ["field_separator", "encoding_characters"], "description": "MSH-1 and MSH-2 are delimiter definitions and are not split." },
        "value":       { "type": "string" },
        "empty":       { "type": "boolean" },
        "repetitions": { "type": "array", "items": { "$ref": "#/$defs/repetition" }, "description": "Empty for empty fields and for MSH-1 / MSH-2." }
      }
    },
    "repetition": {
      "type": "object",
      "additionalProperties": false,
      "required": ["index", "components"],
      "properties": {
        "index":      { "type": "integer", "minimum": 1 },
        "value":      { "type": "string" },
        "empty":      { "type": "boolean" },
        "components": { "type": "array", "items": { "$ref": "#/$defs/component" }, "description": "Empty for an empty repetition." }
      }
    },
    "component": {
      "type": "object",
      "additionalProperties": false,
      "required": ["position"],
      "properties": {
        "position":      { "type": "integer", "minimum": 1 },
        "value":         { "type": "string" },
        "empty":         { "type": "boolean" },
        "subcomponents": { "type": "array", "items": { "$ref": "#/$defs/subcomponent" }, "description": "Only present when the component has more than one subcomponent." }
      }
    },
    "subcomponent": {
      "type": "object",
      "additionalProperties": false,
      "required": ["position", "value"],
      "properties": {
        "position": { "type": "integer", "minimum": 1 },
        "value":    { "type": "string" },
        "empty":    { "type": "boolean" }
      }
    },
    "warning": {
      "type": "object",
      "additionalProperties": false,
      "required": ["code", "message", "segment"],
      "properties": {
        "code": { "type": "string" }, "message": { "type": "string" }, "segment": { "type": ["integer", "null"] }
      }
    }
  }
}
