Use the json module to produce JSON output:

import json

with open(outputfilename, 'wb') as outfile:
    json.dump(row, outfile)

This writes the JSON result directly to the file (replacing any previous content if the file already existed).

If you need the JSON result string in Python itself, use json.dumps() (added s, for 'string'):

json_string = json.dumps(row)

The L is just Python syntax for a long integer value; the json library knows how to handle those values, no L will be written.

Demo string output:

>>> import json
>>> row = [1L,[0.1,0.2],[[1234L,1],[134L,2]]]
>>> json.dumps(row)
'[1, [0.1, 0.2], [[1234, 1], [134, 2]]]'
Answer from Martijn Pieters on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_convert_into_JSON.asp
Python Convert From Python to JSON
Remove List Duplicates Reverse ... ... If you have a Python object, you can convert it into a JSON string by using the json.dumps() method....
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_json.asp
Python JSON
Convert from Python to JSON: import json # a Python object (dict): x = { "name": "John", "age": 30, "city": "New York" } # convert into JSON: y = json.dumps(x) # the result is a JSON string: print(y) Try it Yourself ยป ยท You can convert Python objects of the following types, into JSON strings: dict ยท list ยท
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ convert-python-list-to-json
Convert Python List to Json - GeeksforGeeks
July 23, 2025 - In this example, a Python list containing a mix of integers and strings (list_1) is converted to a JSON-formatted string (json_str) using json.dumps().
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-list-to-json
Python List to JSON
To convert a Python List to JSON, use json.dumps() function. dumps() function takes list as argument and returns a JSON String. In this tutorial, we have examples to demonstrate different scenarios where we convert a given list to JSON string.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ convert python list to json examples
Convert Python List to JSON Examples - Spark By {Examples}
March 27, 2024 - How to convert a list to JSON in Python? You can use the json.dumps() method to convert a Python list to a JSON string. This function takes a list as an argument and returns the JSON value.
Find elsewhere
๐ŸŒ
Tech With Tech
techwithtech.com โ€บ home โ€บ converting list to json array in python: how to?
Converting List to JSON Array in Python: How To? - Tech With Tech
October 27, 2022 - Python lists are the most similar structure to JSON arrays, so they are converted directly to JSON arrays by using the dumps method:
๐ŸŒ
Quora
quora.com โ€บ How-do-I-convert-a-list-to-JSON-in-Python
How to convert a list to JSON in Python - Quora
json.dump(py_list, f, ensure_ascii=False, indent=2) Options: indent formats output, ensure_ascii=False preserves non-ASCII characters. ... Converting a Python list to JSON is straightforward using the built-in json module.
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ convert a list to json string in python [easy step-by-step]
Convert a List to JSON String in Python [Easy Step-By-Step] - AskPython
May 30, 2023 - By converting a Python list to JSON, one can utilise its powerful features. We can use the Python json module to convert the Python list to a JSON string.
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ convert python list to json array | example code
Convert Python list to JSON array | Example code
May 11, 2025 - Simple example code converting given list ["1", "2", "3"] to a JSON array results in the string '["1", "2", "3"]'. For this example, you have to import a JSON module into the project file. import json aList = ["1", "2", "3"] res = json.dumps(aList) ...
๐ŸŒ
Medium
medium.com โ€บ @wepypixel โ€บ how-to-convert-a-list-to-json-python-simple-technique-pypixel-c329f6de5d7a
How to Convert a List to JSON Python? Simple Technique -PyPixel | by Stilest | Medium
August 26, 2023 - With the json module that we imported earlier, we can employ the json.dumps() function to transform the Python list into a JSON-formatted string.
Top answer
1 of 1
1

Instead of creation of separate dictionaries and then merging them, try to create the final (joined) dictionary, without any intermediate step.

The fragment of your code creating concepts list is OK.

Then add import json at the start of your program, and at the end, add the following code:

res = []    # Result
for row in concepts:
    curr = res    # Current object
    for str in row:
        if len(curr) == 0:
            curr.append({})
        curr = curr[0]
        if str not in curr:
            curr[str] = []
        curr = curr[str]

print(json.dumps(res, indent=2))

As you can see, the idea is:

  1. The result (res) is a list contaning a single dictionary object.
  2. Processing of each row causes "going down the object tree", on each string - element of the current row.
  3. Initially added value to a dictionary (for some key) contains an empty list. If there is no "more embedded" element, this ends this "path".
  4. Before "going down the tree" the program adds an empty dictionary to this list, if it has not beed added earlier.
  5. The last step is to add an empty array, under the key equal to the current string.

The printed result (slightly reformatted to take less rows) is:

[{"machine learning": [{
    "inear model": [{
      "linear regression": [{
        "least squares": []}]}],
    "neural network": [{
      "neuron model": [{
        "activation function": []}]}],
    "multi-layer network": [{
      "perceptron": [{
        "connection right": []}]}],
    "reinforcement learning": [{
      "model learning": [{
        "strategy evaluation": [],
        "strategy improvement": []}],
      "model-free learning": [{
        "monte carlo method": [],
        "time series learning": []}]}],
    "imitate learning": [{
      "directly imitate learning": [{
        "inverse reinforcement learning": []}]}]
    }]
}]
๐ŸŒ
Finxter
blog.finxter.com โ€บ 5-best-ways-to-convert-a-python-list-to-json
5 Best Ways to Convert a Python List to JSON โ€“ Be on the Right Side of Change
February 20, 2024 - Bonus Method 5: Using map() with json.dumps(). This one-liner is elegant and useful for quickly transforming and serializing lists of objects by applying a function to each item. It is more functional in style, which may be less readable to some Python programmers.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ json-data-python
Python JSON Data: A Guide With Examples | DataCamp
December 3, 2024 - While Python is capable of storing intricate data structures such as sets and dictionaries, JSON is limited to handling strings, numbers, booleans, arrays, and objects. Letโ€™s look at some of the differences: To convert a Python list to JSON format, you can use the json.dumps() method from ...
๐ŸŒ
DEV Community
dev.to โ€บ wepypixel โ€บ how-to-convert-a-list-to-json-python-best-simple-technique-2oee
How to Convert a List to JSON Python? Best Simple Technique - DEV Community
September 1, 2025 - In this Article, we have converted list to json python of 3 different aspects: List to JSON, List of Lists to JSON, List of Dictionaries to JSON. You can try implementing these in your code editor on your own and see if youโ€™re able to match the outputs.