A
A
Andrey Bezverov2016-11-12 11:43:48
Django
Andrey Bezverov, 2016-11-12 11:43:48

How to write the same fields from json with different values?

"contact_groups": [
          {
            "contacts": [
              {
                "type": "phone",
                "value": "+73903124090",
              },
              {
                "type": "phone",
                "value": "+73903122241",
              },
              {
                "type": "phone",
                "value": "+73903124024",
              }
            ]
          }
        ],

for contacts in listing['contact_groups']:
  for contact in contacts['contacts']:
    if (contact['type'] == 'phone'):
      com_phones = contact['value']
    else:
      com_phones = ''

When parsing with one phone, the data in com_phones is entered without problems.
How to add multiple phones to com_phones?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2016-11-12
@libConfig

for contacts in listing['contact_groups']:
  com_phones = []
  for contact in contacts['contacts']:
    if (contact['type'] == 'phone'):
      com_phones.append(contact['value'])
...

A
Andrey Bezverov, 2016-11-13
@libConfig

Now it looks like this:

com_phones = []
if ('contact_groups' in listing):
   for contacts in listing['contact_groups']:
     for contact in contacts['contacts']:
       if (contact['type'] == 'phone'):
        com_phones.append(contact['value'])
      else:
        com_phones = ''
print(com_phones) # Тут список телефонов

But there was a new plug. Sometimes there are 2-3 "Contacts" blocks. And as I already understood that append'om here to not manage.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question