How to append data to a json file?
json might not be the best choice for on-disk formats; The trouble it has with appending data is a good example of why this might be. Specifically, json objects have a syntax that means the whole object must be read and parsed in order to understand any part of it.
Fortunately, there are lots of other options. A particularly simple one is CSV; which is supported well by python's standard library. The biggest downside is that it only works well for text; it requires additional action on the part of the programmer to convert the values to numbers or other formats, if needed.
Another option which does not have this limitation is to use a sqlite database, which also has built-in support in python. This would probably be a bigger departure from the code you already have, but it more naturally supports the 'modify a little bit' model you are apparently trying to build.
How to append data in json file using typescript?
You probably just miss to specify the encoding:
fs.readFileSync(path, "utf8")
Node.js doc of fs.readFileSync
:
If the
encoding
option is specified then this function returns a string. Otherwise it returns a buffer.
So now you should be able to use it as expected:
interface Data {
price: number;
timestamp: string;
}
let old_data: string = fs.readFileSync(path, "utf8")
// ...
let json_obj: Data[] = JSON.parse(old_data) || [] // No longer need extra array; initialoze as an empty array if needed
json_obj.push(data)
How to append JSON data to existing JSON file node.js
Try this. Don't forget to define anchors
array.
var data = fs.readFileSync('testOutput.json');
var json = JSON.parse(data);
json.push(...anchors);
fs.writeFile("testOutput.json", JSON.stringify(json))
How to append consecutively to a JSON file in Go?
Write the opening [
to the file. Create an encoder on the file. Loop over slices and the elements of each slice. Write a comma if it's not the first slice element. Encode each slice element with the encoder. Write the closing ]
.
_, err := f.WriteString("[")
if err != nil {
log.Fatal(err)
}
e := json.NewEncoder(f)
first := true
for i := 0; i < 10; i++ {
// Create dummy slice data for this iteration.
dataToWrite := []struct {
Id string
Data string
}{
{fmt.Sprintf("id%d.1", i), fmt.Sprintf("data%d.1", i)},
{fmt.Sprintf("id%d.2", i), fmt.Sprintf("data%d.2", i)},
}
// Encode each slice element to the file
for _, v := range dataToWrite {
// Write comma separator if not the first.
if !first {
_, err := f.WriteString(",\n")
if err != nil {
log.Fatal(err)
}
}
first = false
err := e.Encode(v)
if err != nil {
log.Fatal(err)
}
}
}
_, err = f.WriteString("]")
if err != nil {
log.Fatal(err)
}
https://go.dev/play/p/Z-T1nxRIaqL
If it's reasonable to hold all of the slice elements in memory, then simplify the code by encoding all of the data in a single batch:
type Item struct {
Id string
Data string
}
// Collect all items to write in this slice.
var result []Item
for i := 0; i < 10; i++ {
// Generate slice for this iteration.
dataToWrite := []Item{
{fmt.Sprintf("id%d.1", i), fmt.Sprintf("data%d.1", i)},
{fmt.Sprintf("id%d.2", i), fmt.Sprintf("data%d.2", i)},
}
// Append slice generated in this iteration to the result.
result = append(result, dataToWrite...)
}
// Write the result to the file.
err := json.NewEncoder(f).Encode(result)
if err != nil {
log.Fatal(err)
}
https://go.dev/play/p/01xmVZg7ePc
How to append data to a json file on new line using Golang
If you want each JSON object to start on a new line, simple write a newline after each object.
Also note that os.File
has a File.Write()
method to write a []byte
, so no need to convert it to string
.
Also don't forget to close the file, preferably deferred:
f, err := os.OpenFile("./data.json", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
n, err := f.Write(byteArray)
if err != nil {
fmt.Println(n, err)
}
if n, err = f.WriteString("\n"); err != nil {
fmt.Println(n, err)
}
Also note that you could write the byte slice and a newline with a single call like this:
if _, err := fmt.Fprintf(f, "%s\n", byteArray); err != nil {
fmt.Println(err)
}
Appending data to JSON file with python?
So file mode = 'r' is to read the file and file mode = 'w' is to write to file, in the for loop when you start looping it multiple times it should get appended which is file mode = 'a'.If you use 'w' it tries to overwrite the existing text in file.
with open("info_.json", "a") as data:
information = {name: {'surname': surname, 'age': age}}
data.write(json.dumps(information))
data.close()
So, when you make the file mode = 'w' and then execute the for loop for the first time the data gets into file perfectly and when the for loop gets executed second time the data gets overwritten on the previously existing data in file.So file mode='a' is a process where the data gets added/appended into the file for n number of times the for loop runs
How to append data into a json key using python
Your documents
are within the gcsDocuments
dict which is within the inputDocuments
dict (try print(file_data.keys()
) i.e change it to
file_data["inputDocuments"]["gcsDocuments"]["documents"].append(new_data)
python create json file if it doesn't exist otherwise append
You cannot append to a json-file as such. See the json file as one (serialized) python object, in your case a dictionary.
You could load the json file (if it's there), and if not initialise it as en empty dict.
Then add the data that you wish and save it again (in one piece).
import json
import time
fname = 'test.json'
loaded = {}
try:
with open(fname, "r") as f:
loaded = json.load(f)
except IOError:
# may complain to user as well
pass
loaded['appended'] = time.time()
with open(fname, "w") as f:
json.dump(loaded, f)
Related Topics
Why Do -1 and -2 Both Hash to -2 in Cpython
Opencv Real Time Streaming Video Capture Is Slow. How to Drop Frames or Get Synced with Real Time
Python Global Exception Handling
Creating Spark Data Structure from Multiline Record
Parameter Substitution for a SQLite "In" Clause
Error: 'Int' Object Is Not Subscriptable - Python
Split a List into Parts Based on a Set of Indexes in Python
Check What Files Are Open in Python
Force Numpy Ndarray to Take Ownership of Its Memory in Cython
Board-Drawing Code to Move an Oval
How to Display a Pandas Data Frame with Pyqt5/Pyside2
How to Create an Encrypted Zip File
How to Overwrite/Print Over the Current Line in Windows Command Line