I am working with lightning web components and I have static json files. I want to work with them in LWC in my javascript file. its not a zipped file. I want the files to be in static resource because of the cache advantage. I saw examples for css and script files, but not for json.

Any help will be appreciated.

1 Answer 1


This is actually surprisingly easy to achieve using standard JavaScript functionality that is supported by the Salesforce Locker Service; the XMLHttpRequest. You can check what is supported by opening the Locker API Viewer then searching for the feature of interest.

Static resources can be referenced by special imports in LWC using their base name (without their file-type-extension). This import in the LWC component is of the form:

import jsonData from '@salesforce/resourceUrl/data';

This actually defines a variable (here jsonData) that contains the URL to the specified resource.

It is therefore really easy to then use an XMLHttpRequest to load the content of that file into the component. In your case you want to load the file and parse it as JSON.

Here is an example of this in action. First, the JSON file as a static resource "data.json":

[ "hello", "goodbye" ]

Next the "jsonLoader" LWC JavaScript:

import {LightningElement, track} from 'lwc';
import jsonData from '@salesforce/resourceUrl/data';

export default class JsonLoader extends LightningElement {
    // Holds the fetched JSON file's content parsed to an object
    @track jsonToShow;

    // One of the standard lifecycle callbacks you can include in an LWC
    connectedCallback() {
        // Create a request for the JSON data and load it synchronously,
        // parsing the response as JSON into the tracked property
        let request = new XMLHttpRequest();
        request.open("GET", jsonData, false);

        this.jsonToShow = JSON.parse(request.responseText);

This code illustrates a synchronous loading of the data in the connectedCallback, but you could implement this with asynchronous behaviour instead. The JSON is obtained, parsed and stored in a tracked property to ensure that the LWC's template is re-rendered when the value is set or updated.

The LWC's template here is a trivial example:

    <div>The JSON is:</div>

The LWC's metadata exposes this component so it is available in the Lightning App Builder, specifically in home and record pages:

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <description>JSON Loader</description>
    <masterLabel>JSON Loader</masterLabel>

If you add this LWC and the static resource to your org you can then drop this (exposed) component into a home page or record page and will see the text, including the JSON data in that page:

the LWC's rendering

  • I haven't tried this yet, but it closely resembles the cssLoader, and if this works, it will help me update the lightning-tree component via json
    – Greg K.
    May 29, 2022 at 7:35
  • I tried it and it works!
    – Greg K.
    Jun 1, 2022 at 0:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .