Function to create json file

Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 715
First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member

Do we have a function to dump dictionary to a json file?

Tagged:

Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 715
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Answer ✓

    The following function can be used:

        import os
        def DumpToJson(dict, filePath):
            '''
            Dump dictionnary to .json file
            '''
            try:
                with io.open(filePath, "w", encoding="utf8") as json_file:
                    json.dump(dict, json_file, ensure_ascii=False)
                return True
            except:
                ExtAPI.Log.WriteMessage("Error : Exception in DumpToJson()")
                return False