This commit is contained in:
Mike Farah 2022-11-11 14:58:39 +11:00
parent db8a7617ce
commit 245a3dbe34
8 changed files with 95 additions and 8 deletions

View File

@ -16,6 +16,7 @@
* [Add](operators/add.md) * [Add](operators/add.md)
* [Alternative (Default value)](operators/alternative-default-value.md) * [Alternative (Default value)](operators/alternative-default-value.md)
* [Anchor and Alias Operators](operators/anchor-and-alias-operators.md) * [Anchor and Alias Operators](operators/anchor-and-alias-operators.md)
* [Array to Map](operators/array-to-map.md)
* [Assign (Update)](operators/assign-update.md) * [Assign (Update)](operators/assign-update.md)
* [Boolean Operators](operators/boolean-operators.md) * [Boolean Operators](operators/boolean-operators.md)
* [Collect into Array](operators/collect-into-array.md) * [Collect into Array](operators/collect-into-array.md)

28
operators/array-to-map.md Normal file
View File

@ -0,0 +1,28 @@
# Array to Map
Use this operator to convert an array to..a map. The indices are used as map keys, null values in the array are skipped over.
Behind the scenes, this is implemented using reduce:
```
(.[] | select(. != null) ) as $i ireduce({}; .[$i | key] = $i)
```
## Simple example
Given a sample.yml file of:
```yaml
cool:
- null
- null
- hello
```
then
```bash
yq '.cool |= array_to_map' sample.yml
```
will output
```yaml
cool:
2: hello
```

View File

@ -60,7 +60,7 @@ a: 2001-12-15
## Format: get the day of the week ## Format: get the day of the week
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: 2001-12-15T02:59:43.1Z a: 2001-12-15
``` ```
then then
```bash ```bash

View File

@ -337,7 +337,7 @@ Given a sample.yml file of:
a: a:
cool: cool:
foo: bar foo: bar
+id: hi +@id: hi
``` ```
then then
```bash ```bash
@ -357,7 +357,7 @@ Given a sample.yml file of:
a: a:
cool: cool:
foo: bar foo: bar
+id: hi +@id: hi
``` ```
then then
```bash ```bash
@ -375,7 +375,7 @@ Given a sample.yml file of:
a: a:
cool: cool:
foo: bar foo: bar
+id: hi +@id: hi
``` ```
then then
```bash ```bash

View File

@ -80,3 +80,26 @@ will output
- frog - frog
``` ```
## Inserting into the middle of an array
using an expression to find the index
Given a sample.yml file of:
```yaml
- cat
- dog
- frog
- cow
```
then
```bash
yq '(.[] | select(. == "dog") | key + 1) as $pos | .[0:($pos)] + ["rabbit"] + .[$pos:]' sample.yml
```
will output
```yaml
- cat
- dog
- rabbit
- frog
- cow
```

View File

@ -135,6 +135,24 @@ will output
- a: 100 - a: 100
``` ```
## Sort by custom date field
Given a sample.yml file of:
```yaml
- a: 12-Jun-2011
- a: 23-Dec-2010
- a: 10-Aug-2011
```
then
```bash
yq 'with_dtf("02-Jan-2006"; sort_by(.a))' sample.yml
```
will output
```yaml
- a: 23-Dec-2010
- a: 12-Jun-2011
- a: 10-Aug-2011
```
## Sort, nulls come first ## Sort, nulls come first
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -149,6 +149,23 @@ person:
- pizza - pizza
``` ```
## Decode properties - array should be a map
If you have a numeric map key in your property files, use array_to_map to convert them to maps.
Given a sample.properties file of:
```properties
things.10 = mike
```
then
```bash
yq -p=props '.things |= array_to_map' sample.properties
```
will output
```yaml
things:
10: mike
```
## Roundtrip ## Roundtrip
Given a sample.properties file of: Given a sample.properties file of:
```properties ```properties

View File

@ -128,7 +128,7 @@ will output
```yaml ```yaml
+p_xml: version="1.0" encoding="UTF-8" +p_xml: version="1.0" encoding="UTF-8"
cat: cat:
+legs: "4" +@legs: "4"
legs: "7" legs: "7"
``` ```
@ -149,7 +149,7 @@ will output
+p_xml: version="1.0" encoding="UTF-8" +p_xml: version="1.0" encoding="UTF-8"
cat: cat:
+content: meow +content: meow
+legs: "4" +@legs: "4"
``` ```
## Parse xml: custom dtd ## Parse xml: custom dtd
@ -347,7 +347,7 @@ Fields with the matching xml-attribute-prefix are assumed to be attributes.
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
cat: cat:
+name: tiger +@name: tiger
meows: true meows: true
``` ```
@ -368,7 +368,7 @@ Fields with the matching xml-content-name is assumed to be content.
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
cat: cat:
+name: tiger +@name: tiger
+content: cool +content: cool
``` ```