Automating dumping to json - Python in Grasshopper

Hi all, I have a simple question that I can’t seem to find an answer to. I have a workflow in grasshopper that loops through a json, does a bunch of complicated stuff, and then needs to write a unique json for each successive loop, which is initiated by a counter. I’m used to dumping files in Python in other IDEs, however using only json.dump does not seem to work in Grasshopper, as the file doesn’t write anywhere - so I added some rhino script syntax. While this does work as intended, I can’t figure out how to automate this process, as it always prompts for user input to click the save button and complete the process. Can anyone give me insight on what I’m missing here? Example code below:

import json as json
import rhinoscriptsyntax as rs

filename = run_name + '_' + str(b_idx) + '_' + "matching results" 

# enter dictionary key value pairs here
dictionary = {
    "test": x
}

filter = "JSON File (*.json)|*.json|All Files (*.*)|*.*||"
save = rs.SaveFileName("Save JSON file as", filter, filepath, filename)

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

You just need to specify the file path, and open it in the correct mode to use Python’s JSON library:

import json

with open(file_path, 'wt') as f:
    json.dump(f, '{"Hello" : "World"}')

Define file path with a r string on Windows to avoid back slash hell. Use 'at' to append, if the file already exists.

That code is for Rhino.Python. Grasshopper makes if far easier.

Here is a thread that may help:

Note that List access is set for the x input so that the looping can happen all at once for the write.

And then this one to write a file: