# Modify JSON file

**URL:** <https://forum.ansible.com/t/modify-json-file/34249>\
**Category:** Ansible Project\
**Created:** [November 27, 2020, 6:00pm UTC](https://forum.ansible.com/t/modify-json-file/34249 "2020-11-27T18:00:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bilkam](https://avatars.discourse-cdn.com/v4/letter/b/e8c25b/32.png) [@bilkam](https://forum.ansible.com/u/bilkam)\
**Post date:** [November 27, 2020, 6:00pm UTC](https://forum.ansible.com/t/modify-json-file/34249/1 "2020-11-27T18:00:36Z")

</div>

Hello

I have 2 json files:

**File Json1**

{

“name”: “”,

“description”: “”,

“spec”: [

{

“question\_name”: “VM Name”,

“question\_description”: “”,

“required”: true,

“type”: “text”,

“variable”: “VM\_Name”,

“min”: 0,

“max”: 1024,

“default”: “”,

“choices”: “”,

“new\_question”: true

},

{

“question\_name”: “Select a Veeam Backup Job”,

“question\_description”: “”,

“required”: true,

“type”: “multiplechoice”,

“variable”: “Job\_Name”,

“min”: null,

“max”: null,

“default”: “”,

“choices”:

}

]

}

**File Json2**

“Jobs\_List\_Data”:[“JOB-1” , “JOB-2”,“JOB-3”, “JOB-4”]

I’m stuck in modifying the File Json1 with error:

TASK [json\_modify] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

\<127.0.0.1\> ESTABLISH LOCAL CONNECTION FOR USER: root

\<127.0.0.1\> EXEC /bin/sh -c ‘echo ~root && sleep 0’

\<127.0.0.1\> EXEC /bin/sh -c ‘( umask 77 && mkdir -p “`echo /root/.ansible/tmp`”&& mkdir /root/.ansible/tmp/ansible-tmp-1606499676.6535082-233822-207184568804253 && echo ansible-tmp-1606499676.6535082-233822-207184568804253=“`echo /root/.ansible/tmp/ansible-tmp-1606499676.6535082-233822-207184568804253`” ) && sleep 0’

Using module file /usr/local/lib/python3.6/site-packages/ansible/modules/json\_modify.py

\<127.0.0.1\> PUT /root/.ansible/tmp/ansible-local-233716vja5qmf5/tmphostnj3p TO /root/.ansible/tmp/ansible-tmp-1606499676.6535082-233822-207184568804253/AnsiballZ\_json\_modify.py

\<127.0.0.1\> EXEC /bin/sh -c ‘chmod u+x /root/.ansible/tmp/ansible-tmp-1606499676.6535082-233822-207184568804253/ /root/.ansible/tmp/ansible-tmp-1606499676.6535082-233822-207184568804253/AnsiballZ\_json\_modify.py && sleep 0’

\<127.0.0.1\> EXEC /bin/sh -c ‘/usr/bin/python3 /root/.ansible/tmp/ansible-tmp-1606499676.6535082-233822-207184568804253/AnsiballZ\_json\_modify.py && sleep 0’

\<127.0.0.1\> EXEC /bin/sh -c ‘rm -f -r /root/.ansible/tmp/ansible-tmp-1606499676.6535082-233822-207184568804253/ \> /dev/null 2\>&1 && sleep 0’

The full traceback is:

File “/tmp/ansible\_json\_modify\_payload\_zjd6hujn/ansible\_json\_modify\_payload.zip/ansible/modules/json\_modify.py”, line 38, in main

File “/usr/local/lib/python3.6/site-packages/jsonpointer.py”, line 126, in resolve\_pointer

return pointer.resolve(doc, default)

File “/usr/local/lib/python3.6/site-packages/jsonpointer.py”, line 204, in resolve

doc = self.walk(doc, part)

File “/usr/local/lib/python3.6/site-packages/jsonpointer.py”, line 272, in walk

raise JsonPointerException(“index ‘%s’ is out of bounds” % (part, ))

fatal: [localhost]: FAILED! =\> {

“changed”: false,

“invocation”: {

“module\_args”: {

“action”: “update”,

“append”: null,

“data”: {

“description”: “”,

“name”: “”,

“spec”: [

{

“choices”: “”,

“default”: “”,

“max”: 1024,

“min”: 0,

“new\_question”: true,

“question\_description”: “”,

“question\_name”: “VM Name”,

“required”: true,

“type”: “text”,

“variable”: “VM\_Name”

},

{

“choices”: null,

“default”: “”,

“max”: null,

“min”: null,

“question\_description”: “”,

“question\_name”: “Select a Veeam Backup Job”,

“required”: true,

“type”: “multiplechoice”,

“variable”: “Job\_Name”

}

]

},

“extend”: null,

“pointer”: “/spec/2/choices/”,

“update”: {

“Backup\_Jobs\_Lists”: [

“JOB-1”,

" JOB-2",

“JOB-3”,

" JOB-4"

]

}

}

},

**“msg”: “index ‘2’ is out of bounds”**

}

The final json file should be like this

**Final Json:**

{

“name”: “”,

“description”: “”,

“spec”: [

{

“question\_name”: “VM Name”,

“question\_description”: “”,

“required”: true,

“type”: “text”,

“variable”: “VM\_Name”,

“min”: 0,

“max”: 1024,

“default”: “”,

“choices”: “”,

“new\_question”: true

},

{

“question\_name”: “Select a Veeam Backup Job”,

“question\_description”: “”,

“required”: true,

“type”: “multiplechoice”,

“variable”: “Job\_Name”,

“min”: null,

“max”: null,

“default”: “”,

**“choices”: [“JOB-1” , “JOB-2”, “JOB-3”, “JOB-4”]**

}

]

}

Here is my code:

- hosts: localhost

tasks:

- name: load json file

include\_vars:

file: surveyjobs.json

name: json1

- debug:

var: json1

- name: load json file

include\_vars:

file: Jobs\_list.json

name: json2

- debug:

var: json2

- json\_modify:

data: “{{json1}}”

pointer: “/spec/2/choices/”

action: update

update: “{{ json2 }}”

register: result

- debug:

var: result.result

Thanks for your help !

---

<div class="post-metadata">

**Author:** ![bilkam](https://avatars.discourse-cdn.com/v4/letter/b/e8c25b/32.png) [@bilkam](https://forum.ansible.com/u/bilkam)\
**Post date:** [November 30, 2020, 7:27am UTC](https://forum.ansible.com/t/modify-json-file/34249/2 "2020-11-30T07:27:54Z")

</div>

Hello anyone can help me get n this below?
