# FHIRPath Patch

The FHIR standard defines a FHIR-native mechanism for expressing changes to a FHIR resource in terms of only the delta you wish to apply. This syntax is called [FHIRPath Patch](/content/docs/fhir_standard/fhir_patch.html) (also often called FHIR Patch).

A FHIRPath Patch document is a [Parameters](https://hl7.org/fhir/parameters.html) resource with any number of patch operations (add an element, delete an element, etc.). Each operation in the document will be applied in order.

The HAPI FHIR [FhirPatchBuilder](https://hapifhir.io/hapi-fhir/apidocs/hapi-fhir-base/ca/uhn/fhir/util/FhirPatchBuilder.html) class can be helpful as a way of generating FHIR Patch documents.

# Operation: Add

The **Add** operation takes a path to a parent element within a resource (or the name of the resource itself), the name of the element to add, and a value to actually add.

## Example Document

```json
{
  "resourceType": "Parameters",
  "parameter": [ {\
    "name": "operation",\
    "part": [ {\
      "name": "type",\
      "valueString": "add"\
    }, {\
      "name": "path",\
      "valueString": "Patient"\
    }, {\
      "name": "name",\
      "valueString": "identifier"\
    }, {\
      "name": "value",\
      "valueIdentifier": {\
        "system": "http://system",\
        "value": "value-new"\
      }\
    } ]\
  } ]
}
```

Copy

## Example Java

```java
FhirPatchBuilder builder = new FhirPatchBuilder(myContext);
builder
	.add()
	.path("Patient")
	.name("identifier")
	.value(new Identifier().setSystem("http://system").setValue("value-new"));
IBaseParameters patch = builder.build();
```

Copy

# Operation: Insert

The **Insert** operation takes a path to a repeatable element, an index to insert at, and a value to actually insert.

## Example Document

```json
{
  "resourceType": "Parameters",
  "parameter": [ {\
    "name": "operation",\
    "part": [ {\
      "name": "type",\
      "valueString": "insert"\
    }, {\
      "name": "path",\
      "valueString": "Patient.identifier"\
    }, {\
      "name": "index",\
      "valueInteger": 1\
    }, {\
      "name": "value",\
      "valueIdentifier": {\
        "system": "http://system",\
        "value": "value-new"\
      }\
    } ]\
  } ]
}
```

Copy

## Example Java

```java
FhirPatchBuilder builder = new FhirPatchBuilder(myContext);
builder
	.insert()
	.path("Patient.identifier")
	.index(1)
	.value(new Identifier().setSystem("http://system").setValue("value-new"));
IBaseParameters patch = builder.build();
```

Copy

# Operation: Delete

The **Delete** operation takes a path to an element to delete.

By default, the operation will fail if the path matches multiple elements, but this can be changed with the `allowMultipleMatches` option as described in [Deleting Multiple Elements](/content/docs/fhir_standard/fhir_patch.html#multi-delete/index.html) below.

## Example Document

```json
{
  "resourceType": "Parameters",
  "parameter": [ {\
    "name": "operation",\
    "part": [ {\
      "name": "type",\
      "valueString": "delete"\
    }, {\
      "name": "path",\
      "valueString": "Patient.identifier[1]"\
    } ]\
  } ]
}
```

Copy

## Example Java

```java
FhirPatchBuilder builder = new FhirPatchBuilder(myContext);
builder
	.delete()
	.path("Patient.identifier[1]");
IBaseParameters patch = builder.build();
```

Copy

# Deleting Multiple Elements

Per the [FHIR Patch specification](https://hl7.org/fhir/fhirpatch.html), the specified path must only match one element. This can be controlled by adding an additional parameter to the _delete_ operation called `allowMultipleMatches` with a boolean value of `true`.

## Example Document

```json
{
  "resourceType": "Parameters",
  "parameter": [ {\
    "name": "operation",\
    "part": [ {\
      "name": "type",\
      "valueString": "delete"\
    }, {\
      "name": "path",\
      "valueString": "Patient.identifier.where(system='http://system0')"\
    }, {\
      "name": "allowMultipleMatches",\
      "valueBoolean": true\
    } ]\
  } ]
}
```

Copy

## Example Java

```java
FhirPatchBuilder builder = new FhirPatchBuilder(myContext);
builder
	.delete()
	.path("Patient.identifier.where(system='http://system0')")
	.allowMultipleMatches();
IBaseParameters patch = builder.build();
```

Copy

# Operation: Replace

The **Replace** operation takes a path to an element to replace, and a value to actually replace it with. The path must resolve to a single element.

## Example Document

```json
{
  "resourceType": "Parameters",
  "parameter": [ {\
    "name": "operation",\
    "part": [ {\
      "name": "type",\
      "valueString": "replace"\
    }, {\
      "name": "path",\
      "valueString": "Patient.identifier[1]"\
    }, {\
      "name": "value",\
      "valueIdentifier": {\
        "system": "http://system",\
        "value": "value-new"\
      }\
    } ]\
  } ]
}
```

Copy

## Example Java

```java
FhirPatchBuilder builder = new FhirPatchBuilder(myContext);
builder
	.replace()
	.path("Patient.identifier[1]")
	.value(new Identifier().setSystem("http://system").setValue("value-new"));
IBaseParameters patch = builder.build();
```

Copy

# Operation: Move

The move operation takes a path to a repeatable element to move, and a source and destination index.

## Example Document

```json
{
  "resourceType": "Parameters",
  "parameter": [ {\
    "name": "operation",\
    "part": [ {\
      "name": "type",\
      "valueString": "move"\
    }, {\
      "name": "path",\
      "valueString": "Patient.identifier"\
    }, {\
      "name": "source",\
      "valueInteger": 1\
    }, {\
      "name": "destination",\
      "valueInteger": 2\
    } ]\
  } ]
}
```

Copy

## Example Java

```java
FhirPatchBuilder builder = new FhirPatchBuilder(myContext);
builder
	.move()
	.path("Patient.identifier")
	.source(1)
	.destination(2);
IBaseParameters patch = builder.build();
```

Copy

# Example: Replace Any Existing LOINC Codes

The following example shows a FHIR Patch which operated on an Observation resource. It removes any and all values in `Observation.code.coding` with a `Coding.system` value of 'http://loinc.org', and adds a new LOINC code in their place.

This document should always leave exactly one LOINC code in `Observation.code` regardless of whether there was previously no such code, one code, or multiple codes.

```json
{
  "resourceType": "Parameters",
  "parameter": [ {\
    "name": "operation",\
    "part": [ {\
      "name": "type",\
      "valueString": "delete"\
    }, {\
      "name": "path",\
      "valueString": "Observation.code.coding.where(system='http://loinc.org')"\
    }, {\
      "name": "allowMultipleMatches",\
      "valueBoolean": true\
    } ]\
  }, {\
    "name": "operation",\
    "part": [ {\
      "name": "type",\
      "valueString": "add"\
    }, {\
      "name": "path",\
      "valueString": "Observation.code"\
    }, {\
      "name": "name",\
      "valueString": "coding"\
    }, {\
      "name": "value",\
      "valueCoding": {\
        "system": "http://loinc.org",\
        "code": "85354-9",\
        "display": "Blood Pressure"\
      }\
    } ]\
  } ]
}
```

Copy

## Java Example

To build this patch document in Java:

```java
FhirPatchBuilder builder = new FhirPatchBuilder(myContext);
// Delete any existing LOINC codes
builder
	.delete()
	.path("Observation.code.coding.where(system='http://loinc.org')")
	.allowMultipleMatches();
// Add a specific LOINC code
builder
	.add()
	.path("Observation.code")
	.name("coding")
	.value(new Coding("http://loinc.org", "85354-9", "Blood Pressure"));
IBaseParameters patch = builder.build();
```

Copy

[FHIRPath Expressions](/content/docs/fhir_standard/fhirpath_expressions.html)

FHIRPath Patch

The FHIR Standard

[FHIR Introduction](/content/docs/fhir_standard/fhir_introduction.html)
[FHIR Versions](/content/docs/fhir_standard/fhir_versions.html)
[FHIR CRUD Operations](/content/docs/fhir_standard/fhir_crud_operations.html)
[FHIR Searching Basics](/content/docs/fhir_standard/fhir_search_queries.html)
[FHIR Search: References and Includes](/content/docs/fhir_standard/fhir_search_references.html)
[FHIR Search: Custom Search Parameters](/content/docs/fhir_standard/fhir_search_custom_search_parameters.html)
[FHIR Search: Combo Search Parameters](/content/docs/fhir_standard/fhir_search_combo_search_parameters.html)
[FHIR Search: Enforcing Uniqueness](/content/docs/fhir_standard/fhir_search_uniqueness.html)
[FHIR Search: Uplifted Refchains](/content/docs/fhir_standard/fhir_search_uplifted_refchains.html)
[FHIR Transactions and Batches](/content/docs/fhir_standard/transactions.html)
[FHIRPath Expressions](/content/docs/fhir_standard/fhirpath_expressions.html)
[FHIRPath Patch](/content/docs/fhir_standard/fhir_patch.html)
