Can't serialize HTMLScriptElement

I get this error when trying to pass a geojson object from the view context to a map:

django can’t serialize object of unregistered class HTMLScriptElement

in my template I have:

{{top7d_starts|json_script:"top7d_starts"}}

and in my map.js:

var top7d_starts = document.getElementById('top7d_starts');

The error occurs on this code for mapLibre:

map.on('load', () => {
    map.addSource('starts', {
        type: 'geojson',
        data: top7d_starts
    });
});

The geojson object is already serialized.

What steps am I missing? Or is there a better way to bring a geojson object from the view context to the js map?

You probably have to do something like:

const data = JSON.parse(top7d_starts.textContent)

And then pass the data variable, not the script element.

If you’re getting this error on the server, I’d look at what top7d_starts contains.

And if you are getting this error on the server, we’d need to see the view and template to help you diagnose this.