0

I have a string:

[{"leagueId":"37085320-2d31-11e8-802e-c81f66dacb22","leagueName":"Akali's Highwaymen","tier":"BRONZE","queueType":"RANKED_FLEX_SR","rank":"III","playerOrTeamId":"87747329","playerOrTeamName":"REYKKU","leaguePoints":0,"wins":53,"losses":59,"veteran":true,"inactive":false,"freshBlood":false,"hotStreak":false},{"leagueId":"c64de250-0065-11e8-b792-c81f66dd2a8f","leagueName":"Fiora's Horde","tier":"BRONZE","queueType":"RANKED_SOLO_5x5","rank":"IV","playerOrTeamId":"87747329","playerOrTeamName":"REYKKU","leaguePoints":28,"wins":29,"losses":25,"veteran":false,"inactive":false,"freshBlood":false,"hotStreak":false}]

How do I turn this string into ONE array with dictionary and arrays inside it?

I have tried ast.literal_eval() but that only seems to work when it is ONLY a dictionary

2
  • This looks very much like JSON -> try json.loads(), but since the line is relatively long and not broken down, I could have missed something, Commented Jun 24, 2018 at 13:52
  • json.loads() seems to work thanks! Commented Jun 24, 2018 at 21:12

1 Answer 1

2

Use the JSON module.

Ex:

import json
data = """[{"leagueId":"37085320-2d31-11e8-802e-c81f66dacb22","leagueName":"Akali's Highwaymen","tier":"BRONZE","queueType":"RANKED_FLEX_SR","rank":"III","playerOrTeamId":"87747329","playerOrTeamName":"REYKKU","leaguePoints":0,"wins":53,"losses":59,"veteran":true,"inactive":false,"freshBlood":false,"hotStreak":false},{"leagueId":"c64de250-0065-11e8-b792-c81f66dd2a8f","leagueName":"Fiora's Horde","tier":"BRONZE","queueType":"RANKED_SOLO_5x5","rank":"IV","playerOrTeamId":"87747329","playerOrTeamName":"REYKKU","leaguePoints":28,"wins":29,"losses":25,"veteran":false,"inactive":false,"freshBlood":false,"hotStreak":false}]"""
print(json.loads(data))

Output:

[{u'queueType': u'RANKED_FLEX_SR', u'hotStreak': False, u'wins': 53, u'veteran': True, u'losses': 59, u'playerOrTeamId': u'87747329', u'leagueName': u"Akali's Highwaymen", u'playerOrTeamName': u'REYKKU', u'inactive': False, u'rank': u'III', u'freshBlood': False, u'leagueId': u'37085320-2d31-11e8-802e-c81f66dacb22', u'tier': u'BRONZE', u'leaguePoints': 0}, {u'queueType': u'RANKED_SOLO_5x5', u'hotStreak': False, u'wins': 29, u'veteran': False, u'losses': 25, u'playerOrTeamId': u'87747329', u'leagueName': u"Fiora's Horde", u'playerOrTeamName': u'REYKKU', u'inactive': False, u'rank': u'IV', u'freshBlood': False, u'leagueId': u'c64de250-0065-11e8-b792-c81f66dd2a8f', u'tier': u'BRONZE', u'leaguePoints': 28}]
Sign up to request clarification or add additional context in comments.

3 Comments

Why did you use 3 """ at the start and at the end? Does that change the data type? Why 3 instead of 1?
in python triple quotes represent string...."""String""" & '''string'''
So what is the difference between "String" and """String"""

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.