Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

173
Vistas
How to import .js that imported .json from html

I am not an English-speaking citizen, I am not good at English but I did best.

I am trying to import a js file that imported json from html

.js

import JSON from '../json/menu.json';

(function () {
  function parseMenu(ul, menu) {
    for (var i=0;i<menu.length;i++) {
      if(menu.sub == null) {
        menu.sort(function(a, b) {
          return a.id > b.id ? -1 : a.id > b.id ? 1 : 0;
        });
      }
      var li=$(ul).append('<li><a href="'+menu[i].link+'">'+menu[i].name+'</a></li>');
      if (menu[i].sub!=null) {
        var subul=$('<ul id="submenu'+menu[i].link+'"></ul>');
        $(li).append(subul);
        parseMenu($(subul), menu[i].sub);
      }
    }
  }
  var menu=$('#menu');
  parseMenu(menu, JSON);
});

.json form

[ {
     {
     },
     {
     }
   },
   {
     {
     },
     {
     }
   },
   {
   }
]

I tried like

<script type="module" src="../js/left.js"></script>
<script type="module" src="../json/menu.json"></script>

in html

But I get the following error Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "application/json". Strict MIME type checking is enforced for module scripts per HTML spec. in .json

I solved it.

First. Changed name of variable. JSON to menus

Second. Deleted <script type="module" src="../js/left.mjs"></script>

Last. Changed import JSON from '../json/menu.json'; to import menus from '../json/menus.json' assert { type: "json" };

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

My guess is you are actually trying to use the .js as src in this part:

<script type="module" src="../json/menu.json"></script>

Chrome currently supports https://github.com/tc39/proposal-import-assertions. This allows you to tell the browser that you are importing a json file:

import JSON from '../json/menu.json' assert { type: "json" };

By the way you shouldn't use JSON as the name of variable because it is a built-in object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda