Home > Software engineering >  Adding all elements from list into json dict
Adding all elements from list into json dict

Time:05-28

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.

CodePudding user response:

You need to unpack the list into its destination

json_data = {
    "parent": {"foo": "bar"},
    "children": [
        {'existing1': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}},
        {'existing2': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}},
        *values
    ]
}

Or use list.extend

json_data = {
    "parent": {"foo": "bar"},
    "children": [
        {'existing1': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}},
        {'existing2': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}},
    ]
}
json_data['children'].extend(values)

Don't use builtin list keyword for your variable, that's the list constructor

CodePudding user response:

There is one simple way

only use ' '

ex)

    json_data={
  "parent": { "foo": "bar" },
  "children": [
      {'existing1': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}},
      {'existing2': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}}
  ]
}

and have a list that I want to add

list_ =      [
          {'new1': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}},
          {'new2': {'bar': [{'type': 'bar', 'bar': {'content': 'bar'}}]}}
      ]

and

json_data['children']  = list_

result

[{'existing1': {'foo': [{'foo': {'content': 'foo'}, 'type': 'foo'}]}},
 {'existing2': {'foo': [{'foo': {'content': 'foo'}, 'type': 'foo'}]}},
 {'new1': {'foo': [{'foo': {'content': 'foo'}, 'type': 'foo'}]}},
 {'new2': {'bar': [{'bar': {'content': 'bar'}, 'type': 'bar'}]}}]

CodePudding user response:

A quick and simple way to keep it, from Python 3.9 (i mean) you can use *list to extend it on the fly, like this code here.

json_data={
  "parent": { "foo": "bar" },
  "children": [
      {'existing1': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}},
      {'existing2': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}},
      *list
  ]
}

its return a dict like this.

{
    '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'
                }
            }]
        }
    }]
}

CodePudding user response:

Slightly different to other answers as this allows for more entries in the original list:

list_ = [{'new1': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}},
         {'new2': {'bar': [{'type': 'bar', 'bar': {'content': 'bar'}}]}}]

json_data = {"parent": {"foo": "bar"}, "children": []}

for dl in list_:
    for i, v in enumerate(dl.values(), 1):
        json_data["children"].append({f'existing{i}': v})

print(json_data)

Output:

{'parent': {'foo': 'bar'}, 'children': [{'existing1': {'foo': [{'type': 'foo', 'foo': {'content': 'foo'}}]}}, {'existing1': {'bar': [{'type': 'bar', 'bar': {'content': 'bar'}}]}}]}
  • Related