Export JSON format through Python component in grasshopper

Hello Rhino community,

When i try to export, through the below python script, the format isn’t as wished… i linked to JSON formats, the one which i get when export, and the one i want. i simply created the wanted JSON format by copying the data from the panel and pasted into the file. This is also why i don’t really get why it comes out in a way other format?

Anybody encoutnered this before?

import rhinoscriptsyntax as rs
import json

# Export 'x' as a JSON file if 'run' is true and let the user choose the path
if run:
    default_filename = "Element.json"
    default_directory = rs.DocumentPath()
    save_path = rs.SaveFileName("Save JSON file", "JSON Files (*.json)|*.json||", default_directory, default_filename)
    if save_path:
        try:
            # Convert the entire list 'x' to a JSON string
            json_string = json.dumps(x, indent=1, ensure_ascii=False)
            # Manually encode the JSON string as UTF-8
            encoded_json_string = json_string.encode('utf-8')
            # Write the encoded data to the file
            with open(save_path, 'wb') as outfile:  
                outfile.write(encoded_json_string)
            print("JSON file saved successfully.")
        except Exception as e:
            print("Error saving JSON file:", e)

Element_from Grasshopper export.json (1.4 KB)
Element_wanted form.json (1.3 KB)

Grasshopper_Json Extract 2.0.gh (28.9 KB)
Construction.json (157.7 KB)
Element.json (38.0 KB)

Is there a reason that the JSON is encoded in UTF-8? Normally we would leave it Ascii. The newlines are a bit different in the UTF-8

Here is the Guide on JSON writing in Python with simplified write code:

JSON in Rhino.Python

It’s because i need to proces letters as ‘æ, ø, å’. I’m an architect which needs to deal wither exterior walls (ydervæg - in danish). And i can’t avoide these letters, which seems like Ascii can’t process.

Thou i keep getting the following format:
“[ {\n "Node": {\n "Element": {\n "id": "f74a67b0-9da9-4f72-af72-b7da00a67169",\n "name": {\n "Danish": "Ikke bærende indervæg"\n },\n "source": "User",\n "comment": {\n "English": "",\n "Danish": "",\n "German": "",\n "Norwegian": ""\n },\n "enabled": true,\n "excluded_scenarios": \n }\n }\n },\n {\n "Node": {\n "Element": {\n "id": "f74a67b0-9da9-4f72-af72-b7da00a67169",\n "name": {\n "Danish": "Ikke bærende indervæg"\n },\n "source": "User",\n "comment": {\n "English": "",\n "Danish": "",\n "German": "",\n "Norwegian": ""\n },\n "enabled": true,\n "excluded_scenarios": \n }\n }\n },\n {\n "Edge": [\n {\n "ElementToConstruction": {\n "special_conditions": false, \n "id": "3281e288-b275-4729-8aaa-6d7cbea6cf77", \n "amount": 600.0, \n "excluded_scenarios": , \n "enabled": true\n }\n }, \n "f74a67b0-9da9-4f72-af72-b7da00a67169", \n "4ca1ce20-59a2-4625-b0e2-ddf8202aba27"\n ]\n }]”

Instead of:

[  {
    "Node": {
      "Element": {
        "id": "f74a67b0-9da9-4f72-af72-b7da00a67169",
        "name": {
          "Danish": "Ikke bærende indervæg"
        },
        "source": "User",
        "comment": {
          "English": "",
          "Danish": "",
          "German": "",
          "Norwegian": ""
        },
        "enabled": true,
        "excluded_scenarios": []
      }
    }
  },
  {
    "Node": {
      "Element": {
        "id": "f74a67b0-9da9-4f72-af72-b7da00a67169",
        "name": {
          "Danish": "Ikke bærende indervæg"
        },
        "source": "User",
        "comment": {
          "English": "",
          "Danish": "",
          "German": "",
          "Norwegian": ""
        },
        "enabled": true,
        "excluded_scenarios": []
      }
    }
  },
    {
        "Edge": [
            {
                "ElementToConstruction": {
                    "special_conditions": false, 
                    "id": "3281e288-b275-4729-8aaa-6d7cbea6cf77", 
                    "amount": 600.0, 
                    "excluded_scenarios": [], 
                    "enabled": true
                }
            }, 
            "f74a67b0-9da9-4f72-af72-b7da00a67169", 
            "4ca1ce20-59a2-4625-b0e2-ddf8202aba27"
        ]
    }]