Home > Mobile >  How to Insert array inside JSON object?
How to Insert array inside JSON object?

Time:08-02

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.

CodePudding user response:

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

CodePudding user response:

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"
            }
        ]
    }
}

CodePudding user response:

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

  • Related