1

Im use Ansible filter json_query

data:

[
  "just_dir",
  {
     "path": "extend_dir",
     "order": "nginx"
  }
]

I want to get:

[
  {
     "path": "just_dir",
  },
  {
     "path": "extend_dir",
     "order": "nginx"
  }
]
merage([?type(@) == `string`].{path: @}, [?type(@) == `object`])

does not work.

Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45

1 Answers1

1

Unfortunately, it looks like is not possible to merge to 2 arrays

Github issue: jmespath.py#152

Also you need to add a from_json filter before apply it to json_query

---
- hosts: localhost
  gather_facts: no
  vars:
    data: '[
      "just_dir",
      {
         "path": "extend_dir",
         "order": "nginx"
      }
    ]'
  tasks:
   - name: debug just_dir
     debug: msg="{{ data | from_json  | json_query(jmesquery) }}"
     vars:
       jmesquery: "[?type(@) == `string`].{path: @}"

   - name: debug Other data
     debug: msg="{{ data | from_json  | json_query(jmesquery) }}"
     vars:
       jmesquery: "[?type(@) == `object`]"
sadok-f
  • 1,387
  • 15
  • 28