# Complex api json calls

**URL:** <https://forum.ansible.com/t/complex-api-json-calls/30285>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [May 8, 2019, 9:49pm UTC](https://forum.ansible.com/t/complex-api-json-calls/30285 "2019-05-08T21:49:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicholas\_Britton](https://avatars.discourse-cdn.com/v4/letter/n/a587f6/32.png) [@Nicholas\_Britton](https://forum.ansible.com/u/Nicholas_Britton)\
**Post date:** [May 8, 2019, 9:49pm UTC](https://forum.ansible.com/t/complex-api-json-calls/30285/1 "2019-05-08T21:49:56Z")

</div>

I am looking to use the uri module and have been sucessfull with simpler calls, but i am unsure how to get something like the following to work correctly. I have been provided what the json should look like but i am unsure how to build that out via the uri module in the body syntax.

I have also struggled to find how ansible handles nested levels in the body. from examples it looks like its just the indents. But i am not sure, if someone can help shed some light for me it would be much appreciated.

Here is what the json should look like when sent as the payload:

`  
POST [https://fsunix005.pne.ven.veritas.com/netbackup/config/snapshotproviders/configuredplugins/aws/instances](https://fsunix005.pne.ven.veritas.com/netbackup/config/snapshotproviders/configuredplugins/aws/instances)

{

“data”: {

“id”: “someid”,

“type”: “plugininstance”,

“attributes”: {

“configurationAttributes”: [

{

“name”: “accesskey”,

“singlevalue”: “key1”

},

{

“name”: “secretkey”,

“singlevalue”: “skey2”

},

{

“name”: “regions”,

“multivalue”: [

“us-east-1”,

“us-east-2”,

“us-west-1”,

“us-west-2”

]

}

]

}

}

}  
`

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 8, 2019, 9:54pm UTC](https://forum.ansible.com/t/complex-api-json-calls/30285/2 "2019-05-08T21:54:31Z")

</div>

The `body` is typically easiest represented as a YAML representation of the JSON data format. So in your case, this should be what you need:

- uri:  
url: [https://fsunix005.pne.ven.veritas.com/netbackup/config/snapshotproviders/configuredplugins/aws/instances](https://fsunix005.pne.ven.veritas.com/netbackup/config/snapshotproviders/configuredplugins/aws/instances)  
method: POST  
body\_format: json  
body:  
data:  
attributes:  
configurationAttributes:
- name: accesskey  
singlevalue: key1
- name: secretkey  
singlevalue: skey2
- multivalue:
- us-east-1
- us-east-2
- us-west-1
- us-west-2  
name: regions  
id: someid  
type: plugininstance

---

<div class="post-metadata">

**Author:** ![Nicholas\_Britton](https://avatars.discourse-cdn.com/v4/letter/n/a587f6/32.png) [@Nicholas\_Britton](https://forum.ansible.com/u/Nicholas_Britton)\
**Post date:** [May 8, 2019, 10:46pm UTC](https://forum.ansible.com/t/complex-api-json-calls/30285/3 "2019-05-08T22:46:25Z")

</div>

So if the multivalue is driven from user input or an input file and will vary in what regions are to be used, how can that be dynamic?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 13, 2019, 10:09pm UTC](https://forum.ansible.com/t/complex-api-json-calls/30285/4 "2019-05-13T22:09:27Z")

</div>

If you look in the documentation you'll see that you can use json in body by setting body\_format.

File:  
&nbsp;&nbsp;body: "{{ lookup('file', '/path/to/json/file.json' }}"

Variable  
&nbsp;&nbsp;body: "{{ myvar.json }}"
