Home > Software engineering >  Parsing a JSON list into a normal string in Python?
Parsing a JSON list into a normal string in Python?

Time:03-10

I have a JSON list which has this data:

[ "B6y2z5jqUeFXJAre8ugXkHeYX87SU3hi6E", "B9bUMF8VLbj8r2b8uEQ7TY4R6TRDCjjhMV", "BJauzhHkcLKLdFsMuqLh6QrFGCTJjC7RQz", "BLcwoAVZNSLXLBMqQ7PQqEDFhN4owFLJo2", "BMaqYvfgrcZf6Wx1P3P8oBQzwjEHqGAEgy", "BMsXoM73RoxjwCVqbySNPcrVCKQqbLFn12", "BR5ptgmfcYceqka6JLxqkr21ce8J5T6Cvv" ]

I want to take the first thing in the list ("B6y2z5jqUeFXJAre8ugXkHeYX87SU3hi6E") and turn it into a normal Python string. How can I do this?

CodePudding user response:

You can do this:

jsonList = [ 
    "B6y2z5jqUeFXJAre8ugXkHeYX87SU3hi6E", 
    "B9bUMF8VLbj8r2b8uEQ7TY4R6TRDCjjhMV", 
    "BJauzhHkcLKLdFsMuqLh6QrFGCTJjC7RQz", 
    "BLcwoAVZNSLXLBMqQ7PQqEDFhN4owFLJo2", 
    "BMaqYvfgrcZf6Wx1P3P8oBQzwjEHqGAEgy", 
    "BMsXoM73RoxjwCVqbySNPcrVCKQqbLFn12", 
    "BR5ptgmfcYceqka6JLxqkr21ce8J5T6Cvv" ]
#I want to take the first thing in the list ("B6y2z5jqUeFXJAre8ugXkHeYX87SU3hi6E") and turn it into a normal Python string. How can I do this?
s = jsonList[0]
print(f"variable s has type {type(s)} and value '{s}'")

Output:

variable s has type <class 'str'> and value 'B6y2z5jqUeFXJAre8ugXkHeYX87SU3hi6E'

CodePudding user response:

It is already a string. You can do:


# IF:
json_data = [ "B6y2z5jqUeFXJAre8ugXkHeYX87SU3hi6E", "B9bUMF8VLbj8r2b8uEQ7TY4R6TRDCjjhMV", "BJauzhHkcLKLdFsMuqLh6QrFGCTJjC7RQz", "BLcwoAVZNSLXLBMqQ7PQqEDFhN4owFLJo2", "BMaqYvfgrcZf6Wx1P3P8oBQzwjEHqGAEgy", "BMsXoM73RoxjwCVqbySNPcrVCKQqbLFn12", "BR5ptgmfcYceqka6JLxqkr21ce8J5T6Cvv" ]

element_one = json_data[0]
element_two = json_data[1]

The list is a list of strings, and they can be accessed with that.

  • Related