# how to join two json data structure to perform a join operation

**URL:** <https://forum.ansible.com/t/how-to-join-two-json-data-structure-to-perform-a-join-operation/25442>\
**Category:** Ansible Project\
**Created:** [September 8, 2017, 8:55pm UTC](https://forum.ansible.com/t/how-to-join-two-json-data-structure-to-perform-a-join-operation/25442 "2017-09-08T20:55:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris7444](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@Chris7444](https://forum.ansible.com/u/Chris7444)\
**Post date:** [September 8, 2017, 8:55pm UTC](https://forum.ansible.com/t/how-to-join-two-json-data-structure-to-perform-a-join-operation/25442/1 "2017-09-08T20:55:40Z")

</div>

Hi

I am very new with ansible and I am struggling to solve the following “problem”  
I have two json inputs

a list of “locations” (you can think them as stores). For each "location"I have the name and the unique ID

and a list of “parts” . For each item, I have an array which tells me in which “locations” I can find the item

I want to build an array of locations, for each location I want to know the list of “items” available in this location

I don;t know how to do that with ansible

Thank you

Chris

locations with their IDs

{  
“offset”: 0,  
“locations”: [  
{  
“name”: “Dallas”,  
“id”: “42086a89-910f-4903-6db5-1f40995a6920”,  
“foo”: “value1”  
},  
{  
“name”: “Houston”,  
“id”: “42083339-7472-0ed6-3c34-eb51865dd50f”,  
“foo”: “something”  
},  
{  
“name”: “Austin”,  
“id”: “42081ecb-ff7d-20c2-156f-1b748411eadd”,  
“foo”: “sonethingelse”  
}  
],  
“count”: 3,  
“bar”: 500  
}

parts

{  
“foo”: 500,  
“count”: 3,  
“items”: [  
{  
“name”: “item1”,  
“locations”: [  
{  
“id”: “42083339-7472-0ed6-3c34-eb51865dd50f”  
},  
{  
“id”: “42086a89-910f-4903-6db5-1f40995a6920”  
}  
],  
“foobar”: “Yes”  
},  
{  
“name”: “item2”,  
“locations”: [  
{  
“id”: “42083339-7472-0ed6-3c34-eb51865dd50f”  
},  
{  
“id”: “42081ecb-ff7d-20c2-156f-1b748411eadd”  
}  
],  
“foobar”: “No”  
},  
{  
“name”: “item3”,  
“locations”: [  
{  
“id”: “42083339-7472-0ed6-3c34-eb51865dd50f”  
},  
{  
“id”: “42081ecb-ff7d-20c2-156f-1b748411eadd”  
}  
],  
“foobar”: “Sure”  
}  
]  
}

and I want to build this (or a variable that will let me find which parts are available in each location)

{  
“locations”: [  
{  
“name”: “Dallas”,  
“parts”: [  
{“item”: “item1”}  
]  
},  
{  
“name”: “Houston”,  
“parts”: [  
{“item”: “item2”},  
{“item”: “item3”},  
{“item”: “item1”}  
]  
},  
{  
“name”: “Austin”,  
“parts”: [  
{“item”: “item2”},  
{“item”: “item3”}  
]  
}  
]  
}

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [September 11, 2017, 10:27am UTC](https://forum.ansible.com/t/how-to-join-two-json-data-structure-to-perform-a-join-operation/25442/2 "2017-09-11T10:27:45Z")

</div>

Not had to solve this problem myself but I’d have a look at ‘with\_together’ and set\_fact so that you could create a new fact which contains all the information.  
See [http://docs.ansible.com/ansible/latest/playbooks\_loops.html#looping-over-parallel-sets-of-data](http://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-parallel-sets-of-data) for documentation about with\_together.

Hope this helps,

Jon
