xvienxz2
xvienxz2

Reputation: 63

parse json data seperated by commas?

Lets say i have the following json:

{"data":"DOG,CAT,BIRD,SEED,HEART,DRAGON,LINK,SUNSHINE","success":true}

How would I extract the 1st, 3rd, 6th item in "data"? So extract dog, bird, and dragon. These words will always be changing too. I know how to extract the entire thing by doing:

set['data']

But i'm not sure how to extract individually.

Upvotes: 0

Views: 678

Answers (2)

Ostrava
Ostrava

Reputation: 101

Parse the JSON into an object then split a string into an array delimited by (,) comma

class Example(obj):
        def __init__(self, data):
            self.__dict__ = json.loads(data)

    myObject = Example(myJSONData)
    myList = myObject.data.split(",")
    dog = myList[0]
    bird = myList[2]
    dragon = myList[5]

Upvotes: 0

Perennial
Perennial

Reputation: 436

It is a string. You can use the split method. Assuming you have loaded it into python using json.load(), you can do this:

words = set['data'].split(',')
print words

['DOG', 'CAT', 'BIRD', 'SEED', 'HEART', 'DRAGON', 'LINK', 'SUNSHINE']

Check out the string methods to find out all the ways to manipulate them. https://docs.python.org/2/library/string.html

Upvotes: 1

Related Questions