Change the last line to:

temp = temp.append(data, ignore_index = True)

The reason we have to do this is because the append doesn't happen in place. The append method does not modify the data frame. It just returns a new data frame with the result of the append operation.

Edit:

Since writing this answer I have learned that you should never use DataFrame.append inside a loop because it leads to quadratic copying (see this answer).

What you should do instead is first create a list of data frames and then use pd.concat to concatenate them all in a single operation. Like this:

dfs = [] # an empty list to store the data frames
for file in file_list:
    data = pd.read_json(file, lines=True) # read data frame from json file
    dfs.append(data) # append the data frame to the list

temp = pd.concat(dfs, ignore_index=True) # concatenate all the data frames in the list.

This alternative should be considerably faster.

Answer from Juan Estevez on Stack Overflow
๐ŸŒ
YouTube
youtube.com โ€บ watch
Read Single and Multiple Json Files to Pandas DataFrame Python - YouTube
How to read and load json objects and files into pandas dataframe using pandas.read_json and pandas.DataFrame. How to turn dataframe into json file or object...
Published ย  January 7, 2022
Views ย  8K
Discussions

python - Reading multiple nested JSON files into Pandas DataFrame - Stack Overflow
I have a problem writing the code that will read multiple json files from a folder in Python. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Reading multiple JSON files and appending to a dataset using Python / Pandas - Stack Overflow
Here is the code I am trying to run to read multiple JSON files from my directory. contains the names of all the files I am trying to read. for file in range(1, len(file_name... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python: Read several json files from a folder - Stack Overflow
I would like to know how to read several json files from a single folder (without specifying the files names, just that they are json files). Also, it is possible to turn them into a pandas DataF... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - how to import and read multiple json files in pandas? - Stack Overflow
I'm trying to read multiple json files using python. My files look something like this: Inbox Jack message1.json Brad message1.json Charles message1.json Emerson message1.json Luke message1.... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Top answer
1 of 4
120

Note: Line separated json is now supported in read_json (since 0.19.0):

In [31]: pd.read_json('{"a":1,"b":2}\n{"a":3,"b":4}', lines=True)
Out[31]:
   a  b
0  1  2
1  3  4

or with a file/filepath rather than a json string:

pd.read_json(json_file, lines=True)

It's going to depend on the size of you DataFrames which is faster, but another option is to use str.join to smash your multi line "JSON" (Note: it's not valid json), into valid json and use read_json:

In [11]: '[%s]' % ','.join(test.splitlines())
Out[11]: '[{"a":1,"b":2},{"a":3,"b":4}]'

For this tiny example this is slower, if around 100 it's the similar, signicant gains if it's larger...

In [21]: %timeit pd.read_json('[%s]' % ','.join(test.splitlines()))
1000 loops, best of 3: 977 ยตs per loop

In [22]: %timeit l=[ json.loads(l) for l in test.splitlines()]; df = pd.DataFrame(l)
1000 loops, best of 3: 282 ยตs per loop

In [23]: test_100 = '\n'.join([test] * 100)

In [24]: %timeit pd.read_json('[%s]' % ','.join(test_100.splitlines()))
1000 loops, best of 3: 1.25 ms per loop

In [25]: %timeit l = [json.loads(l) for l in test_100.splitlines()]; df = pd.DataFrame(l)
1000 loops, best of 3: 1.25 ms per loop

In [26]: test_1000 = '\n'.join([test] * 1000)

In [27]: %timeit l = [json.loads(l) for l in test_1000.splitlines()]; df = pd.DataFrame(l)
100 loops, best of 3: 9.78 ms per loop

In [28]: %timeit pd.read_json('[%s]' % ','.join(test_1000.splitlines()))
100 loops, best of 3: 3.36 ms per loop

Note: of that time the join is surprisingly fast.

2 of 4
28

If you are trying to save memory, then reading the file a line at a time will be much more memory efficient:

with open('test.json') as f:
    data = pd.DataFrame(json.loads(line) for line in f)

Also, if you import simplejson as json, the compiled C extensions included with simplejson are much faster than the pure-Python json module.

๐ŸŒ
CSDN
devpress.csdn.net โ€บ python โ€บ 630457abc67703293080b809.html
How to read multiple json files into pandas dataframe?_python_Mangs-Python
August 23, 2022 - dfs = [] # an empty list to store the data frames for file in file_list: data = pd.read_json(file, lines=True) # read data frame from json file dfs.append(data) # append the data frame to the list temp = pd.concat(dfs, ignore_index=True) # concatenate all the data frames in the list. This alternative should be considerably faster. ... ้—ฎ้ข˜:ๅฆ‚ไฝ•้‡ๅก‘็†Š็Œซใ€‚็ณปๅˆ— ๅœจๆˆ‘็œ‹ๆฅ,ๅฎƒๅฐฑๅƒ pandas.Series ไธญ็š„ไธ€ไธช้”™่ฏฏใ€‚ a = pd.Series([1,2,3,4]) b = a.reshape(2,2) b b ๆœ‰็ฑปๅž‹ Series ไฝ†ๆ— ๆณ•ๆ˜พ็คบ,ๆœ€ๅŽไธ€ๆก่ฏญๅฅ็ป™ๅ‡บๅผ‚ๅธธ,้žๅธธๅ†—้•ฟ,ๆœ€ๅŽไธ€่กŒๆ˜ฏโ€œTypeError: %d format: a number is required, not numpy.ndarrayโ€ใ€‚ b.sha
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.read_json.html
pandas.read_json โ€” pandas 3.0.1 documentation - PyData |
Convert a JSON string to pandas object. This method reads JSON files or JSON-like data and converts them into pandas objects. It supports a variety of input formats, including line-delimited JSON, compressed files, and various data representations (table, records, index-based, etc.).
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 65249679 โ€บ reading-multiple-json-files-and-appending-to-a-dataset-using-python-pandas
Reading multiple JSON files and appending to a dataset using Python / Pandas - Stack Overflow
Try this assuming file_names is list and pd.read_json() works as expected. df_hold_list = [] # appending dataframe on each loop is a lot of overhead, so collect them first for file in file_names: df_hold_list.append(pd.read_json(file)) df = pd.concat(df_hold_list, axis=0) # check the axis, either 1 or 0 depending on how you want it to look
Find elsewhere
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ how-read-multiple-json-files-same-directory-merge-csv-notario-moral
How to read multiple json files in the same directory and merge them in a single CSV with Python
December 1, 2021 - The python script and other files whitin the same folder MUST have a different name than the files to be merged ... import os import glob import json import pandas as pd import numpy as np import csv from findtools.find_files import (find_files, Match) from pandas.io.json import json_normalize cwd = os.getcwd() path_to_json =cwd #contents = [] File_prefix = 'file-*' dfs = [] # Recursively find all *.json files in **/home/** json_files_pattern = Match(filetype='f', name=File_prefix) found_files = find_files(path='.', match=json_files_pattern) for found_file in found_files: #--------------------
Top answer
1 of 9
84

One option is listing all files in a directory with os.listdir and then finding only those that end in '.json':

import os, json
import pandas as pd

path_to_json = 'somedir/'
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]
print(json_files)  # for me this prints ['foo.json']

Now you can use pandas DataFrame.from_dict to read in the json (a python dictionary at this point) to a pandas dataframe:

montreal_json = pd.DataFrame.from_dict(many_jsons[0])
print montreal_json['features'][0]['geometry']

Prints:

{u'type': u'Point', u'coordinates': [-73.6051013, 45.5115944]}

In this case I had appended some jsons to a list many_jsons. The first json in my list is actually a geojson with some geo data on Montreal. I'm familiar with the content already so I print out the 'geometry' which gives me the lon/lat of Montreal.

The following code sums up everything above:

import os, json
import pandas as pd

# this finds our json files
path_to_json = 'json/'
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]

# here I define my pandas Dataframe with the columns I want to get from the json
jsons_data = pd.DataFrame(columns=['country', 'city', 'long/lat'])

# we need both the json and an index number so use enumerate()
for index, js in enumerate(json_files):
    with open(os.path.join(path_to_json, js)) as json_file:
        json_text = json.load(json_file)

        # here you need to know the layout of your json and each json has to have
        # the same structure (obviously not the structure I have here)
        country = json_text['features'][0]['properties']['country']
        city = json_text['features'][0]['properties']['name']
        lonlat = json_text['features'][0]['geometry']['coordinates']
        # here I push a list of data into a pandas DataFrame at row given by 'index'
        jsons_data.loc[index] = [country, city, lonlat]

# now that we have the pertinent json data in our DataFrame let's look at it
print(jsons_data)

for me this prints:

  country           city                   long/lat
0  Canada  Montreal city  [-73.6051013, 45.5115944]
1  Canada        Toronto  [-79.3849008, 43.6529206]

It may be helpful to know that for this code I had two geojsons in a directory name 'json'. Each json had the following structure:

{"features":
[{"properties":
{"osm_key":"boundary","extent":
[-73.9729016,45.7047897,-73.4734865,45.4100756],
"name":"Montreal city","state":"Quebec","osm_id":1634158,
"osm_type":"R","osm_value":"administrative","country":"Canada"},
"type":"Feature","geometry":
{"type":"Point","coordinates":
[-73.6051013,45.5115944]}}],
"type":"FeatureCollection"}
2 of 9
24

Iterating a (flat) directory is easy with the glob module

from glob import glob

for f_name in glob('foo/*.json'):
    ...

As for reading JSON directly into pandas, see here.

๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ pandas โ€บ pandas_json.asp
Pandas Read JSON
If your JSON code is not in a file, but in a Python Dictionary, you can load it into a DataFrame directly: ... import pandas as pd data = { "Duration":{ "0":60, "1":60, "2":60, "3":45, "4":45, "5":60 }, "Pulse":{ "0":110, "1":117, "2":103, "3":109, "4":117, "5":102 }, "Maxpulse":{ "0":130, "1":145, "2":135, "3":175, "4":148, "5":127 }, "Calories":{ "0":409, "1":479, "2":340, "3":282, "4":406, "5":300 } } df = pd.DataFrame(data) print(df) Try it Yourself ยป
๐ŸŒ
CopyProgramming
copyprogramming.com โ€บ howto โ€บ how-to-import-and-read-multiple-json-files-in-pandas
Python: Reading and Importing Multiple JSON Files using Pandas: A Guide
June 20, 2023 - Reading and Writing JSON Files in Python with Pandas, To read a JSON file via Pandas, we'll utilize the read_json () method and pass it the path to the file we'd like to read. The method returns a Pandas DataFrame that stores data in the form of columns and rows.
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ how-to-read-multiple-json-files-into-pandas-dataframe
How to Read Multiple JSON Files into Pandas Dataframe | Saturn Cloud Blog
August 25, 2023 - In this section, we will focus ... for reading multiple JSON files into Pandas is to use a for loop to iterate over the files and concatenate them into a single dataframe....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-read-json-files-with-pandas
How to Read JSON Files with Pandas? - GeeksforGeeks
May 13, 2025 - In this article, we are going to see how to read multiple data files into pandas, data files are of multiple types, here are a few ways to read multiple files by using the pandas package in python.
๐ŸŒ
Medium
shahparthvi22.medium.com โ€บ merge-multiple-json-files-to-a-dataframe-5e5421c40d06
Merge multiple JSON files to a DATAFRAME | by Parthvi Shah | Medium
May 19, 2020 - We have collected all the tweets from various governors, public + private profiles into various json files. I hope this makes it easier for you. Before starting, Donโ€™t forget to import the libraries. import json import numpy as np import pandas as pd
๐ŸŒ
BMC Software
bmc.com โ€บ blogs โ€บ pandas-read-json-csv-files
Pandas: How To Read CSV & JSON Files โ€“ BMC Software | Blogs
November 20, 2020 - Pandas does not automatically unwind that for you. Here we follow the same procedure as above, except we use pd.read_json() instead of pd.read_csv(). Notice that in this example we put the parameter lines=True because the file is in JSONP format. That means itโ€™s not a valid JSON file. Rather it is a file with multiple JSON records, one right after the other.
๐ŸŒ
Kaggle
kaggle.com โ€บ code โ€บ hamditarek โ€บ merge-multiple-json-files-to-a-dataframe
Merge multiple JSON files to a DATAFRAME
Checking your browser before accessing www.kaggle.com ยท Click here if you are not automatically redirected after 5 seconds
๐ŸŒ
Like Geeks
likegeeks.com โ€บ home โ€บ python โ€บ pandas โ€บ merge multiple json files in python using pandas
Merge Multiple JSON files in Python using Pandas
Read Multiple JSON Files: Loop through your JSON files and read each one into a DataFrame. Concatenate DataFrames: Use pd.concat() to merge these DataFrames into one. Hereโ€™s a sample code snippet to illustrate this process: import pandas as ...
๐ŸŒ
Stack Exchange
datascience.stackexchange.com โ€บ questions โ€บ 137860 โ€บ how-to-efficiently-merge-multiple-csv-and-json-files-into-a-single-dataframe-usi
How to efficiently merge multiple CSV and JSON files into a single DataFrame using Pandas in Python - Data Science Stack Exchange
I am working with multiple data files in a folder where some files are in CSV format and others are in JSON format. I want to combine all of them into a single DataFrame for further analysis. ... dfs = [] for file in files: if file.endswith(".csv"): dfs.append(pd.read_csv(file)) elif file.endswith(".json"): dfs.append(pd.read_json(file))