Question: How would you translate the following condition into a list comprehension?

```
a = list()
if inputs is not None:
   for i in inputs:
      a.append(my_function(i))
else:
   a.append(my_function())

```

I was thinking:

```
a = [my_function(input) for input in inputs else my_function() if inputs is not None]

```

but getting errors.

Note: This was just a pseudo code with pseudo names. Apologies for confusion.

Answer: 
> 
> Question: Is there a way to process the file, without resorting to the hack?
> 
> 
> 

I am assuming that you are using [JSON-lib](http://json-lib.sourceforge.net/) as implied by the `[json-lib]` tag.

I am also assuming that you have encountered this problem in writing the unit tests for your code. (If not ... why not?)

In the JSON-lib APIs, `JSONObject` and `JSONArray` have a common supertype `JSON`, so you should be able to assign a `JSONObject` or `JSONArray` to a variable of type `JSON`. (The `JSON` interface has methods `isObject` and `isArray` that allow you to test if a `JSON` is an object or an array ... but no `asObject` or `asArray` methods.)

However, if your tests actually require a `JSONObject` ... you have a problem. In JSON, the "object" and "array" data structures are fundamentally different. Neither is a conceptual subtype of the other. What your "hack" is doing is *wrapping* the JSON array inside a JSON object. This may or may not be the correct thing to do. (I suspect it isn't. But if it is the correct thing to do, then the "hack" is a reasonable way to do it. You could also construct a `JSONObject` and add the parsed `JSONArray` to it programatically.)

If the code under test is designed / specified to work with a JSON object and not with a JSON array, then the CORRECT solution is to reject the input file as bad input, at some point.

* If your code under test is designed to do the parsing, then it should throw an exception (or whatever) to reject the bad input file. Your unit test should check that it does that.
* If your code under test is designed to take a (parsed) `JSONObject`, then the testcase (the input file containing a JSON array) is a bad testcase. Remove it ... for the purposes of this unit test.

---

In short, the correct solution to your problem is going to depend on what the code you are testing is *supposed to do*.
You can read jsonStr as JSON Object, then convert it to Java Object:

```
    net.sf.json.JSON json = JSONSerializer.toJSON(jsonStr);
    YourJavaObject jObj = null;
    List<YourJavaObject> list  = null;
    if(json instanceof JSONObject){
        jObj = (YourJavaObject)JSONObject.toBean(JSONObject.fromObject(json), YourJavaObject.class);
    }else if(json instanceof JSONArray){
        list = JSONArray.toList(JSONArray.fromObject(json), new YourJavaObject(), new JsonConfig());
    }

```