Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update JSON subsection #246

Merged
merged 8 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions docs/basic_training/channels.md
Original file line number Diff line number Diff line change
Expand Up @@ -601,23 +601,40 @@ Channel

### JSON

We can also easily parse the JSON file format using the following groovy schema:
We can also easily parse the JSON file format using the `splitJson` channel operator.

The `splitJson` operator supports JSON arrays:
```groovy linenums="1"
import groovy.json.JsonSlurper

def f = file('data/meta/regions.json')
def records = new JsonSlurper().parse(f)

Channel
.of('["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]')
.splitJson()
.view { "Item: ${it}" }
```

for (def entry : records) {
log.info "$entry.patient_id -- $entry.feature"
}
JSON objects:
```groovy linenums="1"
Channel
.of('{"player": {"name": "Bob", "height": 180, "champion": false}}')
.splitJson()
.view { "Item: ${it}" }
```

!!! warning
And even a JSON array of JSON objects!
```groovy linenums="1"
Channel
.of('[{"name": "Bob", "height": 180, "champion": false}, \
{"name": "Alice", "height": 170, "champion": false}]')
.splitJson()
.view { "Item: ${it}" }
```

When using an older JSON version, you may need to replace `parse(f)` with `parseText(f.text)`
Files containing JSON content can also be parsed:
```groovy linenums="1"
Channel
.fromPath('file.json')
.splitJson()
.view { "Item: ${it}" }
```

### YAML
ewels marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
39 changes: 28 additions & 11 deletions docs/basic_training/channels.pt.md
Original file line number Diff line number Diff line change
Expand Up @@ -602,23 +602,40 @@ Channel

### JSON

Também podemos analisar facilmente o formato de arquivo JSON usando o seguinte esquema do Groovy:
Também podemos analisar facilmente o formato de arquivo JSON usando o oeprador de canal `splitJson`.

O operador `splitJson` suporta arranjos JSON:
```groovy linenums="1"
import groovy.json.JsonSlurper

def f = file('data/meta/regions.json')
def registros = new JsonSlurper().parse(f)

Channel
.of('["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"]')
.splitJson()
.view { "Item: ${it}" }
```

for (def entrada : registros) {
log.info "$entrada.patient_id -- $entrada.feature"
}
Objetos JSON:
```groovy linenums="1"
Channel
.of('{"jogador": {"nome": "Bob", "altura": 180, "venceu_campeonato": false}}')
.splitJson()
.view { "Item: ${it}" }
```

!!! warning
E inclusive arranjos JSON com objetos JSON!
```groovy linenums="1"
Channel
.of('[{"nome": "Bob", "altura": 180, "venceu_campeonato": false}, \
{"nome": "Alice", "height": 170, "venceu_campeonato": false}]')
.splitJson()
.view { "Item: ${it}" }
```

Ao usar uma versão JSON mais antiga, pode ser necessário substituir `parse(f)` por `parseText(f.text)`
Arquivos contendo dados em formato JSON também podem ser analisados:
```groovy linenums="1"
Channel
.fromPath('arquivo.json')
.splitJson()
.view { "Item: ${it}" }
```

### YAML

Expand Down
Loading