Skip to content

Commit

Permalink
Merge pull request #246 from mribeirodantas/update_json_subsection
Browse files Browse the repository at this point in the history
Update JSON subsection
  • Loading branch information
ewels authored Jun 27, 2023
2 parents f6b0d0c + 94e9964 commit 315e177
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 22 deletions.
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

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

0 comments on commit 315e177

Please sign in to comment.