An open API service indexing awesome lists of open source software.

https://github.com/cxa/keypathjson

Safe and key-path-able JSON navigator for System.Json
https://github.com/cxa/keypathjson

Last synced: 9 days ago
JSON representation

Safe and key-path-able JSON navigator for System.Json

Awesome Lists containing this project

README

          

# KeyPathJson

Safe and key-path-able JSON navigator for `System.Json`.

(For navigating `System.Text.Json`, use [JPath](https://github.com/cxa/JPath))

## Install

Simply drop `KeyPathJson.fsproj` or `KeyPathJson.fs` to your project, or search `KeyPathJson` on NuGet.

## Example

Given a JSON like this

```json
{
"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{ "value": "New", "onclick": "CreateNewDoc()" },
{ "value": "Open", "onclick": "OpenDoc()" },
{ "value": "Close", "onclick": "CloseDoc()" }
]
}
}
}
```

Say you need to access the second `menuitem`'s `value`, with `KeyPathJson` this is a piece of cake:

```fsharp
open KeyPathJson

// result is a Result
let result =
Json.parse jsonStr
|> Json.string "menu.popup.menuitem.1.value"

```

One thing to aware is that you must use a number as a key to access array, like `menuitem.1` in the above example.

Except accessing string, `KeyPathJson` also provides:

```fsharp
val value :
keyPath:string ->
jsonValue:JsonValue -> Result

val bool :
keyPath:string -> jsonValue:JsonValue -> Result

val byte :
keyPath:string -> jsonValue:JsonValue -> Result

val sbyte :
keyPath:string -> jsonValue:JsonValue -> Result

val int8 :
keyPath:string -> jsonValue:JsonValue -> Result

val uint8 :
keyPath:string -> jsonValue:JsonValue -> Result

val int16 :
keyPath:string -> jsonValue:JsonValue -> Result

val uint16 :
keyPath:string -> jsonValue:JsonValue -> Result

val int32 :
keyPath:string -> jsonValue:JsonValue -> Result

val uint32 :
keyPath:string -> jsonValue:JsonValue -> Result

val int64 :
keyPath:string -> jsonValue:JsonValue -> Result

val uint64 :
keyPath:string -> jsonValue:JsonValue -> Result

val decimal :
keyPath:string -> jsonValue:JsonValue -> Result

val double :
keyPath:string -> jsonValue:JsonValue -> Result

val float32 :
keyPath:string -> jsonValue:JsonValue -> Result

val char :
keyPath:string -> jsonValue:JsonValue -> Result

val string :
keyPath:string -> jsonValue:JsonValue -> Result

val dateTime :
keyPath:string ->
jsonValue:JsonValue -> Result

val dateTimeOffset :
keyPath:string ->
jsonValue:JsonValue -> Result

val guid :
keyPath:string ->
jsonValue:JsonValue -> Result

val timeSpan :
keyPath:string ->
jsonValue:JsonValue -> Result

val uri :
keyPath:string ->
jsonValue:JsonValue -> Result
```

That's all.

## Usage

Drag `src/KeyPathJson.fs` to your project or get it from NuGet:

## License

MIT