Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/stla/jsonStrings

Manipulate JSON strings in R.
https://github.com/stla/jsonStrings

json r rcpp

Last synced: 30 days ago
JSON representation

Manipulate JSON strings in R.

Awesome Lists containing this project

README

        

---
title: "jsonStrings"
output: github_document
---

[![R-CMD-check](https://github.com/stla/jsonStrings/workflows/R-CMD-check/badge.svg)](https://github.com/stla/jsonStrings/actions)

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, collapse = TRUE)
```

```{r package}
library(jsonStrings)
```

## Define a JSON string

```{r jsonstring}
jstring <- jsonString$new("
{
\"foo\": \"hello\",
\"bar\": {\"x\": 1, \"y\": 2},
\"baz\": [9, 99, null],
\"qux\": [null, [0, 1], {\"a\": 1000}]
}
")
```

## Extract a JSON value

```{r}
jstring$at("foo")
jstring$at("bar", "y")
jstring$at("baz", 2)
```

## Erase a JSON value

```{r}
jstring$erase("baz")
jstring
```

## Check existence of a property

```{r}
jstring$hasKey("bar")
```

## Check a type

```{r}
jstring$is("object")
```

## Add a property

```{r}
jstring$addProperty("new", "[4,5]")
jstring
```

## Update a JSON string

```{r}
jstring$update(
"{
\"foo\": \"goodbye\",
\"quux\": 10000
}"
)
jstring
```

## Patch a JSON string

```{r}
jspatch <- "[
{\"op\": \"remove\", \"path\": \"/foo\"},
{\"op\": \"replace\", \"path\": \"/qux/2\", \"value\": 9999}
]"
jstring$patch(jspatch)
```

## Chaining

```{r}
jstring <- jsonString$new("
{
\"foo\": \"hello\",
\"bar\": {\"x\": 1, \"y\": 2},
\"baz\": [9, 99, null],
\"qux\": [null, [0, 1], {\"a\": 1000}]
}
")
jstring$erase("baz")$addProperty("new", "[4,5]")$update(
"{
\"foo\": \"goodbye\",
\"quux\": 10000
}"
)
jstring
```