Function to create json file

Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
100 Answers 500 Comments 250 Likes First Anniversary
✭✭✭✭

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

Tagged:

Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭
    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