0

I have a loop where I add a generated JSON object into a file, and then I separate every object with comma (I use open("filename.txt", "w") method for this). What can you suggest so instead of appending JSON objects to *.txt files I could create multiple JSON files as is with every loop? In the future, I will need to use data stored in JSON to make some calculations, so I am looking for a way to create a well organized structure. Thanks in advance.

6
  • 1
    Why don't you extract data from you JSON obj and store it in a database ? Commented Jul 17, 2017 at 13:28
  • First, I need to create this data - that's why I'm looking for a way to create a JSON files. For now, it looks like this: (generate_data, 10) - and as output I have 10 json objects in a txt file. If I can extract data from this file and you can tell me how I can do it, I will appreciate it. Commented Jul 17, 2017 at 13:30
  • this is all standard python looping and control structures. The internet is full of excellent sources on how to do this in Python for example Commented Jul 17, 2017 at 13:32
  • 1
    If you want the data stored, use a JSON like database. Try MongoDB. Commented Jul 17, 2017 at 13:33
  • @AliceJarmusch, ApriOri is rigth, MongoDB should be a good solution Commented Jul 17, 2017 at 13:38

2 Answers 2

1

If you really want to output the files, you can just dump them separately using Python's json module (docs) like so:

import json
json.dump(json_object, filename)

This will give a you a lot of files you'll need to read back in if needed, using json.load().

You can also save several json objects to one text file and then iterate over them, reading each individual item in using json.loads; note the s at the end, which means it will convert any string you feed into it, for instance a line in your file.

 for line in f:
     input_json = json.loads(line)
     #you'll want to store this somewhere, e.g. in a dictionary. 

But as pointed out in the comments above, you might want to re-consider if that is the path you want to go down.

Sign up to request clarification or add additional context in comments.

Comments

1

Let's consider your storage system is with files.

I would write data only at the end and construct a list of data.

import json
results = []
# Your loop
foreach element in elements:
    element_json_object = ... # Construct a JSON object here
    results.append(element_json_object)

data = {
    "results": results
}

with open('data.json', 'w') as outfile:
    json.dump(data, outfile)

Each time, you will have to load all your JSON file if you want to do calculation. Then if you want to update your file, you will need to load everything, update your JSON data and then save again your file.

This solution is costly, that's why people recommends you to use DataBase like MangoDB.

But depends on what you have to do (school project?), file storage can be enough.

Below a sample for the update.

results = []
with open('data.json') as data_file:    
    data = json.load(data_file)
    results = data["results"]
    # You can update your data here
    for result in results:
        if result["id_test"] == 15:
            result["data_test"] = "DATA UPDATED"
 data = {
    "results": results
}

with open('data.json', 'w') as outfile:
    json.dump(data, outfile)

2 Comments

How to empty the json file before wrting data ?
It is already the case with 'w' w'' Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file. w+'' Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.