How to copy .json and write to .json with user input replacing code in the new file

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By reapersremorse

im using a project to learn to code, and in my getting anywhere lol

im trying to make a .json file generator that will read from a folder that users can easily interact with.
the .jsons in this folder will be like prefabrications. so i need to copy the information from this file and immediately create a file in another directory that has some changed information based on what the user input in the project settings.

i tried putting this in a code block but it kept kicking it out. im doing something wrong here

EX: { "group": "CHANGE", "type": "CHANGE", "pattern": [ "0 2", "345", "678" ], "key": { "0": { "item": "CHANGE" }, "2": { "item": "CHANGE" }, "3": { "item": "CHANGE" }, "4": { "item": "CHANGE" }, "5": { "item": "CHANGE" }, "6": { "item": "CHANGE" }, "7": { "item": "CHANGE" }, "8": { "item": "CHANGE" } }, "result": { "item": "CHANGE", "count": 1 } }
`
this file has 12 different things to change and that’s not uncommon. when you have over 200 items in your mod alone this gets tedious…
but this is a recipe and they arent that bad compared to the blocks in the game.

ill be setting up some sort of spreadsheet with all the blocks, items, potions, effects, etc etc from minecraft. that comes to a total of 2400 entries so ill have to get creative on caching that.

that example .json looked right before i posted…

reapersremorse | 2020-09-21 10:18

I don’t have the faintest idea what your question is about. It’s confusing. Can you explain your problem more specifically? Which code you used, which issue you had, and what you expected?

Zylann | 2020-09-21 12:20

sorry i didnt realize, i need help figuring out how i create a new file from an existing file but things in the new file get replaced by user input

for example
{
“variants”: {
“”: {
“model”: “MOD_ID:block/MOD_OBJECT_block”
}
}
}
the MOD_ID and the MOD_OBJECT would change depending on the users settings and user input

the MOD_ID would be set in a file at the start of the project
the MOD_OBJECT would be set when the user is creating this .json class

i was about dead when i made that post, so im sorry for the confusion.

so basically how do i copy a .json file then replace some keywords in the new file (WITHOUT EDITING THE ORIGINAL) with user input, then create the new file… then use one of the user input strings as the new name of the .json file you just created.

im sorry if its still confusing for people, im not good at putting words in a coherent paragraph. my mind wanders.

reapersremorse | 2020-09-21 15:59

maybe something like this???

func _readJsonsItem():
var file = File.new()
file.open("user://prefabs/assets/MOD_ID/models/block/block.json", File.READ)
var content = file.get_as_text()
file.close()
return content

func _writeJsonModelItem():
now here i change the key words in the file
then save it as a new .json file

reapersremorse | 2020-09-21 16:18