Andres Urrego Angel
Andres Urrego Angel

Reputation: 1932

Python 3 reading json values as tuple

I have created a file parameter for my python script. My idea is read these parameters from a JSON file and then execute wherever I need.

here is the sample file:

{
"configfiles": [
        {
            "log": "/home/ubuntu/scripts/mtm-etl/etl-process-log",
            "connection":"/home/ubuntu/scripts/mtm-etl/etlconfig/mtm-connection",
            "query_redshift_library":"/home/ubuntu/scripts/mtm-etl/etlconfig/query_redshift_library"
        }
    ]
}

Ok, now I pretend to read these key, values in a for within my script to assign them to a variable then, here is my code:

 with open('etl-parameters','r') as userinfo:
        param = json.load(userinfo)

    for param,extraction in param.items():
        if ("ETL" in param):
            for item in extraction:
                process = item['etl-process']
                stored_query = item['stored-query']
        elif( "configfiles"  in param):
            for item in extraction:
                logfile = item['log'],
                connectionfile = item['connection'],
                query_redshift_library = item['query_redshift_library']

My issue is in the elif because for one variable is being assigned the right data type as a string but for some reason for the variables logfile and connectionfile it's assigning a tuple. I have catched up this in my debug:

enter image description here enter image description here

I appreciate your feedback

thanks

Upvotes: 2

Views: 570

Answers (1)

zezollo
zezollo

Reputation: 5027

The error is caused by extraneous commas there:

            logfile = item['log'],
            connectionfile = item['connection'],

Remove them and you'll get your variables as str:

            logfile = item['log']
            connectionfile = item['connection']

As to why this is working this way: python interprets your lines ending with a comma as if you were indeed assigning single element tuples to your variables (parentheses are actually not required), like in this more simple example:

>>> a = 5,
>>> print(a)
(5,)
>>> print(str(type(a)))
<class 'tuple'>
>>>

Upvotes: 3

Related Questions