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

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”}
]
}
]
}

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 for documentation about with_together.

Hope this helps,

Jon