Skip to content

Commit 9cb6cb9

Browse files
committed
Documentation for JSON patch in the dotnet x tool.
1 parent 6e0a4ae commit 9cb6cb9

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

MyApp/_pages/dotnet-tool.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,89 @@ for your Environment that anyone can write to their current directory with **the
269269
`x gist <gist-id>`
270270
:::
271271

272+
### Patch JSON files
273+
274+
The `x` dotnet tool also includes a number of utilities for patching JSON files, e.g. you can use `x patch` to patch a JSON file with a [JSON Patch](https://learn.microsoft.com/en-us/aspnet/core/web-api/jsonpatch?view=aspnetcore-7.0).
275+
276+
The `json.patch` file supports the following JSON Patch operations:
277+
278+
| Operation | Notes |
279+
|-----------|------------------------------------------------------------------------------------|
280+
| add | Add a property or array element. For existing property: set value. |
281+
| remove | Remove a property or array element. |
282+
| replace | Same as remove followed by add at same location. |
283+
| move | Same as remove from source followed by add to destination using value from source. |
284+
| copy | Same as add to destination using value from source. |
285+
| test | Return success status code if value at path = provided value. |
286+
287+
For example, we could have the following `original.json` file:
288+
289+
```json
290+
{
291+
"Logging": {
292+
"LogLevel": {
293+
"Default": "Information",
294+
"Microsoft.AspNetCore": "Warning"
295+
}
296+
},
297+
"smtp": {}
298+
}
299+
```
300+
301+
We need to fill this `smtp` object with settings such as username, password, host, port, and more. To automate filling these values, we can use the ServiceStack `x` tool to apply a `json.patch`. The `json.patch` file to accomplish this would look something like:
302+
303+
```json
304+
[
305+
{
306+
"op": "add",
307+
"path": "/smtp",
308+
"value": {
309+
"UserName": "AWS_ACCESS_KEY_ID",
310+
"Password": "AWS_SECRET_ACCESS_KEY",
311+
"Host": "email-smtp.us-east-1.amazonaws.com",
312+
"Port": 587,
313+
"From": "email address",
314+
"FromName": "From Name",
315+
"Bcc": "bcc email address"
316+
}
317+
}
318+
]
319+
```
320+
321+
Once this patch is applied, our `appsettings.json` transforms into:
322+
323+
```json
324+
{
325+
"Logging": {
326+
"LogLevel": {
327+
"Default": "Information",
328+
"Microsoft.AspNetCore": "Warning"
329+
}
330+
},
331+
"smtp": {
332+
"UserName": "AWS_ACCESS_KEY_ID",
333+
"Password": "AWS_SECRET_ACCESS_KEY",
334+
"Host": "email-smtp.us-east-1.amazonaws.com",
335+
"Port": 587,
336+
"From": "email address",
337+
"FromName": "From Name",
338+
"Bcc": "bcc email address"
339+
}
340+
}
341+
```
342+
343+
You can apply this patch using the `x` tool's `patch` command:
344+
345+
```bash
346+
x patch appsettings.json.patch
347+
```
348+
349+
This expects both the `appsettings.json.patch` and `appsettings.json` files to be local. Optionally, you can specify both files if their names differ.
350+
351+
```bash
352+
x patch changes.json.patch appsettings.json
353+
```
354+
272355
### Lisp REPL
273356

274357
Lisp's dynamism and extensibility makes it particularly well suited for explanatory programming whose access via a REPL is available

0 commit comments

Comments
 (0)