0

I'm creating an application using python. I make a HTTP request and get a following result:

{
    "companies": {
        "company": [
            {
                "createDt": "2014-01-18T00:00:00+01:00",
                "dbNazev": "sveatlo_s_r_o_",
                "id": "1",
                "licenseGroup": "null",
                "nazev": "Sveatlo s.r.o.",
                "show": "true",
                "stavEnum": "ESTABLISHED",
                "watchingChanges": "false"
            },
            {
                "createDt": "2014-01-20T00:00:00+01:00",
                "dbNazev": "hajocka",
                "id": "2",
                "licenseGroup": "null",
                "nazev": "HájoÄka",
                "show": "true",
                "stavEnum": "ESTABLISHED",
                "watchingChanges": "false"
            }
        ]
    }
}

Then I'm processing the data in a for loop. the problem is that the response may also look like this:

{
    "companies": {
        "company": {
            "createDt": "2014-01-18T00:00:00+01:00",
            "dbNazev": "sveatlo_s_r_o_",
            "id": "1",
            "licenseGroup": "null",
            "nazev": "Sveatlo s.r.o.",
            "show": "true",
            "stavEnum": "ESTABLISHED",
            "watchingChanges": "false"
        }
    }
}

Currently I'm checking if it is an array or not after each request like this, but I feel it's not the best way to do this. Could anybody please help me to find a better solution?

Thanks for any answer.

edit: I cannot change the server's response

3
  • Would be helpful to post the code with which you access the json. Commented Jan 24, 2014 at 17:49
  • are you using the json module? Commented Jan 24, 2014 at 17:51
  • I'm using ujson, but the JSON-string to python object is made by requests module build-in functionality Commented Jan 24, 2014 at 17:58

2 Answers 2

5

Normalize the information you're parsing. This will prevent you having to repeat yourself:

Say you have data that could either look like this:

some_json_data = {"companies": {"foo": 1}}

or like this:

some_json_data = {"companies": [{"foo": 1}]}

So when you parse the json data, do this:

i_expect_an_array = some_json_data['companies']
if not isinstance(i_expect_an_array, list):
    i_expect_an_array = (i_expect_an_array,)

# Now, you can process the data as you normally would

No need for two separate blocks of similar looking code.

Sign up to request clarification or add additional context in comments.

7 Comments

Its better tu use tuple instead of list
Tuple is faster than list. Also server response is not changeable thing, so its better logic to don't allow to change it.
Considering that any tiny speed improvements from using a tuple will be subsumed by the time it takes to convert the input list to a tuple, I don't think it makes much of a difference.
You don't need to convert list to tuple, just dict to tuple!
@Lukas: Touché, I misunderstood your comment.
|
2

Convert not list to list or tuple and then loop:

def make_list(item):
    if not isinstance(item, (list, tuple)):
        return [item]
    return list(item)

for company in make_list(data['companies']['company']):
    process_company(company)

1 Comment

Shouldn't make_list be named make_tuple?

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.