Modify JSON file

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 !

Hello anyone can help me get n this below?