0

I have code that is selecting some data from my database.

It returns a list:

selects = [(2, '4'), (2, '17'), (3, '65'), (1, '54'), (2, '14'),...]

I want to transform this list into a JSON object, so I do:

selects = json.dumps(selects)

Giving me a value like this:

[[2, "4"], [2, "17"], [3, "65"], [1, "54"], [2, "14"], ...]

After that, I want to write a for loop to get each object of the dictionary and save into a var. For example:

[[2, "4"]]
var1 = 2
var2 = "4"

But I'm having a problem with extracting the value that I want. When I try var = json[0], it only returns the [.

My idea is to make a for to run thought all the objects in the json, saving them into a var, and sending to another process...

What should I do to save the values like the example above?

Here is the code that I'm using:

import json

class Pull():
    def process(self, **kwargs):
        data     = kwargs.get('data')
        client   = kwargs.get('client')
        postgres = kwargs.get('postgres')

        selects = postgres.select(data_type='session')
        selects = json.dumps(selects)

        for select in selects:
            var1 = select[0]
            var2 = select[1]
            sessions = client.get_sessions(city=var1, cinema=var2)
0

3 Answers 3

1

Part of the confusion here is the terminology, and part of it is an unnecessary level of complexity. Both can be solved.

The JSON object you have is not a dictionary. It is a string, containing the serialized representation of a nested list. As, you would expect from a string, select[0] is the first character of the string, select[1] the second, etc.

When you serialize, whether to JSON or another format, you get something that can be sent or recorded. The JSON data is not meant to be used as data. You have the original, unserialized form to work with if you need the actual list:

selects = postgres.select(data_type='session')

for select in selects:
    var1 = select[0]
    var2 = select[1]
    sessions = client.get_sessions(city=var1, cinema=var2)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help and explanation. Since I'm a newbie in this kinds of problem, it really help me exaplaning the problem with patience.
@a223w2d. Glad it worked out for you. Everyone has trouble when they start. Just keep plugging away until you git gud.
1

It's the 3rd time in 2 days that json.dumps is used for no apparent reason.... selects is already a list / dictionary, there is no need to pass it to json.dumps that returns a string (hence select[0] no returns the first character).

Remove the line selects = json.dumps(selects)

Comments

1

selects = json.dumps(selects) is a string. Doing selects[0] gives the first character.

You don't want to serialize your datas into JSON to process them. Keep your data as they actually are and treat them like a python list.

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.