string is a JSON object or JSON array in Python?
<p>I am reading JSON file line by line. Few lines contain JSON objects while other contains JSON array. I am using <code>json.loads(line)</code> function to get JSON from each line. </p>

<pre><code>def read_json_file(file_name):
    json_file = []
    with open(file_name) as f:
        for line in f:
            json_file.append((line))

    json_array = []
    for obj in json_file:
        try:
            json_array.append(json.loads(obj))
        except ValueError:
            print("data was not valid JSON")

    return json_array
</code></pre>

<p>Is there any way that I can find out that object I am reading is JSON Object or JSON array? I want to save all the result in json_array.</p>

<p>I will be thankful to you if anyone can help me.</p>

<p>In python, JSON object is converted into <code>dict</code> and JSON list is converted into <code>list</code> datatypes.</p>

<p>So, if you want to check the line content which should be valid <strong>JSON</strong>, is <code>JSON Object</code> or <code>JSON Array</code>, then this code will helps you:-</p>

<pre><code>import json

# assume that, each line is valid json data
obj = json.loads(line)

# if returns true, then JSON Array
isintance(obj, list)

# if returns true, then JSON Object.
isintance(obj, dict)
</code></pre>

python json python-3.x