@@ -510,6 +510,82 @@ the page. If you use a dynamic import to lazily-load a JavaScript file and that
510
510
file imports a CSS file (using the non-dynamic ``import `` syntax), that CSS file
511
511
will also be downloaded asynchronously.
512
512
513
+ Importing JSON files
514
+ --------------------
515
+
516
+ .. versionadded :: 7.4
517
+
518
+ Support for importing JSON assets was added in Symfony 7.4.
519
+
520
+ AssetMapper allows you to import JSON assets directly from your JavaScript code.
521
+ While modern browsers support the native ``import data from './foo.json' with { type: 'json' } `` syntax,
522
+ this feature is not yet widely supported. AssetMapper provides a fully compatible alternative
523
+ without requiring any bundler.
524
+
525
+ AssetMapper provides a more compatible alternative by allowing you to import JSON
526
+ files using the standard import syntax:
527
+
528
+ .. code-block :: javascript
529
+
530
+ // assets/app.js
531
+ import dataPromise from ' ./data.json' ;
532
+
533
+ // The import returns a Promise that resolves to the JSON content
534
+ const data = await dataPromise;
535
+ console .log (data .name ); // Access your JSON data
536
+
537
+ Usage Example
538
+ ~~~~~~~~~~~~~
539
+
540
+ Consider a JSON file containing user data:
541
+
542
+ .. code-block :: json
543
+
544
+ // assets/data/users.json
545
+ {
546
+ "users" : [
547
+ {"id" : 1 , "name" : " Alice" , "email" : " [email protected] " },
548
+ {"id" : 2 , "name" : " Bob" , "email" : " [email protected] " }
549
+ ]
550
+ }
551
+
552
+ You can import and use this data in your JavaScript:
553
+
554
+ .. code-block :: javascript
555
+
556
+ // assets/controllers/user-list-controller.js
557
+ import usersPromise from ' ../data/users.json' ;
558
+
559
+ async function displayUsers () {
560
+ const userData = await usersPromise;
561
+
562
+ userData .users .forEach (user => {
563
+ console .log (` ${ user .name } : ${ user .email } ` );
564
+ });
565
+ }
566
+
567
+ displayUsers ();
568
+
569
+ How it Works
570
+ ~~~~~~~~~~~~
571
+
572
+ When you import a JSON file, AssetMapper:
573
+
574
+ 1. **Detects the JSON import ** in your JavaScript files during asset processing
575
+ 2. **Creates a JavaScript module ** that exports a Promise resolving to the JSON content
576
+ 3. **Adds it to the importmap ** so your browser can locate and load it correctly
577
+ 4. **Versions the file ** like any other asset, ensuring proper cache busting
578
+
579
+ This approach works in all modern browsers without requiring native JSON import
580
+ support, making it a reliable alternative to the newer ``with { type: 'json' } ``
581
+ syntax.
582
+
583
+ .. note ::
584
+
585
+ Like CSS imports, JSON imports are processed by AssetMapper and added to the
586
+ importmap automatically. The imported JSON assets are also versioned, so any
587
+ changes to the JSON content will result in a new versioned filename.
588
+
513
589
Issues and Debugging
514
590
--------------------
515
591
0 commit comments