0

I need to make a request to the api of my client, and the api returns this data:

[6,0,'VT3zrYA',5,'USUeZWA',5,0,0,0,0,0,4,0,0,0,2,0,0,3,0,0,0,0,2,0,1,["portale.titolari.client.config.ShoulderDTO/4121330600","java.util.HashSet/3273092938","MATTEO SBRAGIA","java.util.ArrayList/4159755760","java.util.Date/3385151746","MATTEO"],0,7]

How can I parse this data and extract the following fields :

MATTEO SBRAGIA
MATTEO

I've tried this code, but it's not working :

data = json.load(output_data)
pprint data
6
  • 1
    what is the result of your print statement? it also doesn't really look like your data is in json format.... Commented May 28, 2016 at 14:47
  • Does the incoming record have a fixed number of fields? Show us what you've tried? Commented May 28, 2016 at 14:49
  • yes is same position every time Commented May 28, 2016 at 14:50
  • @Matt i want to extract names like my example Commented May 28, 2016 at 14:51
  • That doesn't answer my question. What is the output of your print data command? Commented May 28, 2016 at 14:53

1 Answer 1

2

This in fact is not a valid JSON string because it contains single quotes '. You can replace all single quotes with double quotes and then parse the string but it's a question whether this was intentional or a mistake:

import json

s = '[6,0,\'VT3zrYA\',5,\'USUeZWA\',5,0,0,0,0,0,4,0,0,0,2,0,0,3,0,0,0,0,2,0,1,["portale.titolari.client.config.ShoulderDTO/4121330600","java.util.HashSet/3273092938","MATTEO SBRAGIA","java.util.ArrayList/4159755760","java.util.Date/3385151746","MATTEO"],0,7]'

data = json.loads(s.replace("\'", '"'))

print(data[26][2])
print(data[26][5])

prints:

$ python test.py 
MATTEO SBRAGIA
MATTEO
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.