0

So i have the following json data:

'{"city":"Guarulhos","bot-origin":null,"campaign-source":"attendance bot","lastState":"errorBob","main-installation-date":null,"userid":"c0405312-d97c-4b58-9192-11152c1ee621@tunnel.msging.net","full-name":"Magda dos Santos Silva","alternative-installation-date":null,"chosen-product":"Internet","bank":null,"postalcode":"07013100","due-date":null,"cpf":"45612338813","origin-link":null,"payment":null,"state":"SP","api-orders-hash-id":null,"email":"mah.gata2011@gmail.com","plan-name":"125 Mega","userphone":"11 95287-1231","plan-offer":"687","completed-address":"07013100 - RUA PADRE CELESTINO, 44 - CENTRO, Guarulhos - SP","type-of-person":"CPF","type-of-product":"Residencial","main-installation-period-day":null,"plan-value":"R$89,99","alternative-installation-period-day":null}'

And i want to select only the values "userphone": Phone number(All phone number have the same format) So basically the json response with the phone number. I know i should use a regex but i am not accustomed to then would appreciate any help, specially when there is so many similar values that could interlapse.

Is worth noting i need to interact through a whole column of those json values:

Here is my code:

import re

pattern = re.compile(r'"userphone:"')
phonenumbers=[]
for i in userphone_df['Extras']: 
    phonenumbers.append(pattern.finditer(i))

I was mostly attempting that is

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
INGl0R1AM0R1
  • 1,532
  • 5
  • 16
  • 1
    I am certain Python has a way to parse JSON. You're better off using that, instead of regex. – VLAZ Jan 10 '22 at 15:16

1 Answers1

3

Use json package

import json
d = json.loads('{"city":"Guarulhos","bot-origin":null,"campaign-source":"attendance bot","lastState":"errorBob","main-installation-date":null,"userid":"c0405312-d97c-4b58-9192-11152c1ee621@tunnel.msging.net","full-name":"Magda dos Santos Silva","alternative-installation-date":null,"chosen-product":"Internet","bank":null,"postalcode":"07013100","due-date":null,"cpf":"45612338813","origin-link":null,"payment":null,"state":"SP","api-orders-hash-id":null,"email":"mah.gata2011@gmail.com","plan-name":"125 Mega","userphone":"11 95287-1231","plan-offer":"687","completed-address":"07013100 - RUA PADRE CELESTINO, 44 - CENTRO, Guarulhos - SP","type-of-person":"CPF","type-of-product":"Residencial","main-installation-period-day":null,"plan-value":"R$89,99","alternative-installation-period-day":null}')
print(d['userphone'])

Output:

11 95287-1231
bottledmind
  • 603
  • 3
  • 10
  • Well it certainly is working but i ran into another issue sometime it retuns none values, even tho it shouldnt, i made sure that all columns contained userphone. Nonetheless thank you for the answer – INGl0R1AM0R1 Jan 10 '22 at 16:00
  • @INGl0R1AM0R1 You can make one more question with an example of your issue – bottledmind Jan 10 '22 at 16:08