#How to append a string to an existing json file
34 messages · Page 1 of 1 (latest)
Appending to or just editing a json is not possible as far as I know
You'd have to unmarshal it to a struct or some [string]interface{} map, add it there and then marshal it again
Then you may be overwriting it all
Can you post your code?
Do you write it back to file somewhere?
package main
import (
"encoding/json"
"fmt"
)
func main() {
jsonData1 := `{
"Values":[
{"Min":0.1,"Max":0.2},
{"Min":0.5,"Max":0.65},
{"Min":0.2,"Max":0.3}
]
}`
var unmarshaledJson1 map[string]interface{}
err := json.Unmarshal([]byte(jsonData1), &unmarshaledJson1)
if err != nil {
fmt.Println("Error decoding JSON:", err)
return
}
unmarshaledJson1["sample"] = "new value"
marshaledJsonDataWithAppendedValue, err := json.Marshal(unmarshaledJson1)
if err != nil {
fmt.Println("Error encoding JSON:", err)
return
}
fmt.Println(string(marshaledJsonDataWithAppendedValue))
}
Output
{"Values":[{"Max":0.2,"Min":0.1},{"Max":0.65,"Min":0.5},{"Max":0.3,"Min":0.2}],"sample":"new value"}
looks completely normal to me
ran on https://goplay.tools
Better Go Playground with syntax highlight support
read the separate file with the same method so you would get new map[string]interface{} and I guess foreach would be good for that
for k, v := range separateData {
sample_json[k] = value
}
//marshal sample_json
unmarshal the input data, unmarshal the json file, merge the data with for loop.
marshal the final, merged data and write the result back to the file, overwriting the old content.
to overwrite, you need to truncate the file first.
or os.create should be able to rewrite it
//todo don't get confused by shitty variable naming
separate data was supposed to be your other json if I understood correctly
look at it now, I edited it with some self explaining names
forget about the name, its not important.
4 main point:
- input json, where the new data come from (could be any shape)
- json file that you want to update
- marshal
- unmarshal
yes it was
it does not matter, the variable is just a placeholder for the input data that you need to replace with yours.
jsonData1, err:= os.ReadFile("path to your json file")
if err!=nil {panic(err)}
the rest of the code stay the same.
the solution to this would be strings.split() to remove whitespaces and then something like path.join() to make a proper path out of it but it still begs a question: how do you even get something like that in a path a where are you taking your path from
Open file in truncate mode, read current json, unmarshal it, replace the values, marshal it back to the file
the fact that there is \t and \n means there is something wrong.
why don't you just copy and paste the file path from file manager?
where is the file path come from?
show it
Not trying to be rude but every time you come here with really simple problems that could be solved with some googling, and when we try to help/spoonfeed you, you provide vague informations which aren’t helpful at all
You should learn how to ask questions, provide clear context an d so on, so we can help you a bit
shit happens
it's a magic sentence like "for educational purposes only" in drug making tutorials
we all tried.
you changed something on your code and it doesn't work. but you didn't show us your code, how could we help you to spot the mistake?
you faced an error. but you didn't show us the error message. what do you expect from us?
none of us tied to help ppl.
🤨
it's /n, not \n which would be an escape character
😂
@noble crescent your problem is not ours, we are kind enough to help you.
you should at least, payback with kind and respecful words.
if you're stressed and you cant handle it, dont make it into a hassle for others. go to sleep and rest.
Guy is so mad he’s sending child p**n in my dms