# Using dictionary and json with deserialize in RouterOS

**URL:** <https://forum.ansible.com/t/using-dictionary-and-json-with-deserialize-in-routeros/40171>\
**Category:** Get Help\
**Tags:** routeros\
**Created:** [February 11, 2025, 4:00am UTC](https://forum.ansible.com/t/using-dictionary-and-json-with-deserialize-in-routeros/40171 "2025-02-11T04:00:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![asandres](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@asandres](https://forum.ansible.com/u/asandres)\
**Post date:** [February 11, 2025, 4:00am UTC](https://forum.ansible.com/t/using-dictionary-and-json-with-deserialize-in-routeros/40171/1 "2025-02-11T04:00:18Z")

</div>

Hello everyone. I’m trying to use a list of dictionaries, then use plug-in “to\_json” to easy configuration. Using key/value, will produce less code and save some time, in my case.

The goal is:

- Get some data from inventory as dict, list or a combination of them;
- Pipe to “to\_json” plugin
- Deserialize it inside RouterOS
- Put the result after the RouterOS command

Below there is a simple example playbook:

```auto
---
- name: Test
  hosts: test
  gather_facts: false
  vars:
    ntp_servers:
      address:
        - 192.168.1.2
        - 192.168.1.3
  tasks:
    - name: Add NTP servers
      community.routeros.command:
        commands:
          - /system/ntp/client/servers/add [:deserialize from=json value="{{ ntp_servers | to_json }}"]

```

The problem is that deserialize demands double quotes in value= parameter, and community.routeros.command is escaping it ’ \" ’ , pointing it as an error:

```auto
changed: [test-host] => {
    "changed": true,
    "invocation": {
        "module_args": {
            "commands": [
                "/system/ntp/client/servers/add [:deserialize from=json value=\"{\"address\": [\"192.168.1.2\", \"192.168.1.3\"]}\" ]"
            ],
            "interval": 1,
            "match": "all",
            "retries": 10,
            "wait_for": null
        }
    },
    "stdout": [
        "expected end of command (line 1 column 32)"
    ],
    "stdout_lines": [
        [
            "expected end of command (line 1 column 32)"
        ]
    ]
}

```

Later I will use something like below to ensure idempotent and avoid put a command that potentially cause misconfigurations.

```auto
when: (ntp_config | map('to_nice_json') | list | difference(ntp_servers_registered | map('to_nice_json') | list)) | length != 0

```

Any suggestions?

---

<div class="post-metadata">

**Author:** ![asandres](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@asandres](https://forum.ansible.com/u/asandres)\
**Post date:** [February 11, 2025, 7:36pm UTC](https://forum.ansible.com/t/using-dictionary-and-json-with-deserialize-in-routeros/40171/2 "2025-02-11T19:36:07Z")

</div>

Update:

Tried to execute the command directly on CLI:

```auto
> :put [:deserialize from=json value="[{\"address\":\"192.168.1.2\"}]"]
address=192.168.1.2
> /system/ntp/client/servers/set :put [:deserialize from=json value="[{\"address\":\"192.168.1.2\"}]"]
syntax error (line 1 column 32)

```

The problem is, the command set demands at least one of the following:

```auto
> /system/ntp/client/servers/set
address auth-key comment disabled iburst max-poll min-poll numbers

```

and I’m trying to use deserialize directly without using a loop like:

```auto
community.routeros.command:
   /system/ntp/client/servers/set {{item.key}}={{item.value}}
loop: "{{ ntp_config | map('dict2items') | flatten }}"

```

Didn’t try above code. Later will test and post results.
