I have a list with a couple nested elements in it. For example:
list=[{'new1': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}},
{'new2': {'bar': [{'type': 'bar', 'bar': {'content': 'bar'}}]}}]
I also have a dict for some json data I need to submit via requests
.
For example, this works just fine
json_data={
"parent": { "foo": "bar" },
"children": [
{'existing1': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}},
{'existing2': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}}
]
}
requests.post(url, headers=common_headers, data=json.dumps(json_data))
What I'm trying to do is add all elements from the list
into the json_data
. For example, if I add a single element from the list, it works fine
json_data={
"parent": { "foo": "bar" },
"children": [
{'existing1': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}},
{'existing2': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}},
list[0]
]
}
Because it turns into this
json_data={
"parent": { "foo": "bar" },
"children": [
{'existing1': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}},
{'existing2': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}},
{'new1': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}}
]
}
However, if I add the entire list, it includes the brackets []
and fails. For example, this:
json_data={
"parent": { "foo": "bar" },
"children": [
{'existing1': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}},
{'existing2': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}},
list
]
}
Turns into
json_data={
"parent": { "foo": "bar" },
"children": [
{'existing1': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}},
{'existing2': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}},
[
{'new1': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}},
{'new2': {'bar': [{'type': 'bar', 'bar': {'content': 'bar'}}]}}
]
]
}
The square brackets are breaking the request. Because I don't know how many elements will be in the list, I can't define which element to use (like in the first example).
Is there an easy way for me to include all elements of the list, without the square brackets?
Thank you.