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 all 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
92 changes: 81 additions & 11 deletions docs/basic_training/channels.md
Original file line number Diff line number Diff line change
Expand Up @@ -601,23 +601,93 @@ 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.

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

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

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

for (def entry : records) {
log.info "$entry.patient_id -- $entry.feature"
}
```
=== "Output"

!!! warning
```console
Item: Sunday
Item: Monday
Item: Tuesday
Item: Wednesday
Item: Thursday
Item: Friday
Item: Saturday
```

JSON objects:

=== "Source code"

```groovy linenums="1"
Channel
.of('{"player": {"name": "Bob", "height": 180, "champion": false}}')
.splitJson()
.view { "Item: ${it}" }
```

=== "Output"

```console
Item: [key:player, value:[name:Bob, height:180, champion:false]]
```

When using an older JSON version, you may need to replace `parse(f)` with `parseText(f.text)`
And even a JSON array of JSON objects!

=== "Source code"

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

=== "Output"

```console
Item: [name:Bob, height:180, champion:false]
Item: [name:Alice, height:170, champion:false]
```

Files containing JSON content can also be parsed:

=== "Source code"

```groovy linenums="1"
Channel
.fromPath('file.json')
.splitJson()
.view { "Item: ${it}" }
```

=== "file.json"

```json
[
{ "name": "Bob", "height": 180, "champion": false },
{ "name": "Alice", "height": 170, "champion": false }
]
```

=== "Output"

```console
Item: [name:Bob, height:180, champion:false]
Item: [name:Alice, height:170, champion:false]
```

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

Expand Down
89 changes: 78 additions & 11 deletions docs/basic_training/channels.pt.md
Original file line number Diff line number Diff line change
Expand Up @@ -602,23 +602,90 @@ 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`.

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

def f = file('data/meta/regions.json')
def registros = new JsonSlurper().parse(f)
=== "Código-fonte"

```groovy linenums="1"
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"
}
```
=== "Saída"

!!! warning
```console
Item: Domingo
Item: Segunda
Item: Terça
Item: Quarta
Item: Quinta
Item: Sexta
Item: Sábado
```

Objetos JSON:

=== "Código-fonte"

```groovy linenums="1"
Channel
.of('{"jogador": {"nome": "Bob", "altura": 180, "venceu_campeonato": false}}')
.splitJson()
.view { "Item: ${it}" }
```

=== "Saída"

```console
Item: [key:jogador, value:[nome:Bob, altura:180, venceu_campeonato:false]]
```

Ao usar uma versão JSON mais antiga, pode ser necessário substituir `parse(f)` por `parseText(f.text)`
E inclusive arranjos JSON com objetos JSON!

=== "Código-fonte"

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

=== "Saída"

```console
Item: [nome:Bob, altura:180, venceu_campeonato:false]
Item: [nome:Alice, altura:170, venceu_campeonato:false]
```

Arquivos contendo dados em formato JSON também podem ser analisados:

=== "Código-fonte"

```groovy linenums="1"
Channel
.fromPath('arquivo.json')
.splitJson()
.view { "Item: ${it}" }
```

=== "arquivo.json"

```json
[{"nome": "Bob", "altura": 180, "venceu_campeonato": false}, {"nome": "Alice", "altura": 170, "venceu_campeonato": false}]
```

=== "Saída"

```console
Item: [nome:Bob, altura:180, venceu_campeonato:false]
Item: [nome:Alice, altura:170, venceu_campeonato:false]
```

### YAML

Expand Down
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ markdown_extensions:
base_path: ["."]
- pymdownx.snippets
- pymdownx.superfences
- pymdownx.tabbed:
alternate_style: true
- tables
- toc:
title: On this page
Expand Down