nestedjson2sql is a Python tool for converting JSON data into SQL tables. It dynamically detects column names for each table, handles nested JSON elements, and ensures no duplicates are inserted. This tool is particularly useful for dealing with JSON data that needs to be stored and queried in a relational database format.
To install nestedjson2sql, run the following command:
pip install nestedjson2sqlTo convert a JSON file to SQL, run:
json2sql --file path/to/yourfile.json --db yourdatabase.dbThis will process the JSON file and create corresponding tables in the specified SQLite database.
nestedjson2sql can also process multiple JSON files at once:
json2sql --file file1.json file2.json file3.json --db yourdatabase.dbThis will combine and process the JSON data from all specified files and store it in the SQLite database.
nestedjson2sql offers an additional command line argument --root to specify the name of the main table that will contain the non-nested elements of your JSON data.
You can specify the main table name using the --root parameter. If not specified, it defaults to log.
Example command:
json2sql --file path/to/yourfile.json --db yourdatabase.db --root main_table
In this example, the non-nested elements of the JSON file will be stored in a table named main_table in the specified SQLite database.
The tool can handle two types of JSON files:
- Regular JSON File: A file containing a single JSON object.
- Multi-line JSON File: A file where each line is a valid JSON object.
-
Single JSON File:
If
data.jsoncontains a single JSON object, simply run:json2sql --file data.json --db database.db
-
Multi-line JSON File:
nestedjson2sqlwill detect the multi-line json format. Simply run:json2sql --file multi_line_json.txt --db database.db
-
Multiple Files:
To process multiple files, for example,
data1.json,multi_line_json.txt, anddata3.json, use:json2sql --file data1.json multi_line_json.txt data3.json --db database.db
-
Map all nested elements to root level: Add the
--pin_rootflag :json2sql --file data1.json multi_line_json.txt data3.json --db database.db --pin_root
Contributions to nestedjson2sql are welcome! Please feel free to submit pull requests or open issues to improve the tool or suggest new features.