1

I have a json file that I need to reformat - insert array around some of the nested objects.

It looks like that:

    {
      "test": {
        "UserData": "test",
        "password": "123"
      },
      "test2": {
        "UserData": "test2",
        "password": "123"
      },
      "test3": {
        "UserData": "test3",
        "password": "123"
      }
   }

And I need it to look like that:

    {
      "test": [{
        "UserData": "test",
        "password": "123"
      }],
      "test2": [{
        "UserData": "test2",
        "password": "123"
      }],
      "test3": [{
        "UserData": "test3",
        "password": "123"
      }]
    }

I'm using Ansible, jq and bash.

3 Answers 3

2

Given the data

  d1:
    test:
      UserData: test
      password: '123'
    test2:
      UserData: test2
      password: '123'
    test3:
      UserData: test3
      password: '123'

Convert the dictionaries to the first items in the lists. For example

  d2_query: '[].[key, [value]]'
  d2: "{{ dict(d1|dict2items|json_query(d2_query)) }}"

gives

  d2:
    test:
    - UserData: test
      password: '123'
    test2:
    - UserData: test2
      password: '123'
    test3:
    - UserData: test3
      password: '123'

Example of a complete playbook for testing

- hosts: localhost
  vars:
    d1:
      test:
        UserData: test
        password: '123'
      test2:
        UserData: test2
        password: '123'
      test3:
        UserData: test3
        password: '123'
    d2_query: '[].[key, [value]]'
    d2: "{{ dict(d1|dict2items|json_query(d2_query)) }}"
  tasks:
    - debug:
        var: d2
Sign up to request clarification or add additional context in comments.

Comments

2

Using jq: Select each field of the object with .[], and update it |= to itself wrapped in an array [.].

jq '.[] |= [.]' file.json
{
  "test": [
    {
      "UserData": "test",
      "password": "123"
    }
  ],
  "test2": [
    {
      "UserData": "test2",
      "password": "123"
    }
  ],
  "test3": [
    {
      "UserData": "test3",
      "password": "123"
    }
  ]
}

Demo

1 Comment

jq is great tool used with shell module, but the problem is shell module is not idempotent
1

you could use this playbook (using dict2items and combine):

- name: "tips4"
  hosts: localhost
  vars:
    json1:
      test:
        UserData: test
        password: '123'
      test2:
        UserData: test2
        password: '123'
      test3:
        UserData: test3
        password: '123'   
  tasks:
    - name: set var 
      set_fact:
        newjson: "{{newjson|d({})|combine({item.key:[item.value]}) }}"
      loop: "{{ json1|dict2items }}"

    - name: disp result
      debug:
        msg: "{{ newjson }}"

result:

ok: [localhost] => {
    "msg": {
        "test": [
            {
                "UserData": "test",
                "password": "123"
            }
        ],
        "test2": [
            {
                "UserData": "test2",
                "password": "123"
            }
        ],
        "test3": [
            {
                "UserData": "test3",
                "password": "123"
            }
        ]
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.