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
- Host: GitHub
- URL: https://github.com/cxa/keypathjson
- Owner: cxa
- License: mit
- Created: 2018-02-06T05:39:27.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-09-03T14:09:47.000Z (almost 6 years ago)
- Last Synced: 2025-01-12T14:45:50.624Z (over 1 year ago)
- Language: F#
- Size: 9.77 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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