1

Below is my JSON and I want it to parse and display the contents in a table in LWC like below. I used the JSONtoApex tool to create the parser class but I am not getting in which format I should return my data to LWC jS file so that I can iterate the list/array and display the data in the format shown in below image? I am able to add the individual inner JSON elements (identity,policy..) in to list but I want them to be in a single datatype to be able to iterate in js

 ResultDataJsonParse obj = ResultDataJsonParse.parse(responseBody);
 List<ResultDataJsonParse.detail> detailsList = new List<ResultDataJsonParse.detail>();
 List<ResultDataJsonParse.contract> contractsList = new List<ResultDataJsonParse.contract>();
 for(ResultDataJsonParse.content con:obj.content ){
            detailsList.add(con.vehicle);   
        }

enter image description here

{
  "content": [
    {
      "identity": {
        "caseId": "7546040",
        "caseType": "SMR"
      },
      "policy": {
        "createDate": "1669620011000",
        "modifyDate": "1670398216000",
        "statusCode": "NI"
      },
      "customer": {
        "customerId": "865208",
        "name": "KE PROTEZIONI SOLARI S.R.L."
      },
      "vehicle": {
        "registrationNumber": "FX446DX",
        "vehicleId": "AAFN3045"
      }
    },
    {
      "identity": {
        "caseId": "7546040",
        "caseType": "SMR"
      },
      "policy": {
        "createDate": "1669620011000",
        "modifyDate": "1670398216000",
        "statusCode": "NI"
      },
      "customer": {
        "customerId": "865208",
        "name": "KE PROTEZIONI SOLARI S.R.L."
      },
      "vehicle": {
        "registrationNumber": "FX446DX",
        "vehicleId": "AAFN3045"
      }
    }
  ],
  "first": "true",
  "numberOfElements": "22"
}

1 Answer 1

1

There is no need for a parser class in this case. It is possible to directly pass this string to LWC and convert it to object/array in JS.

But if you want to use Apex then return it as a string from apex using below method :

System.JSON.serialize(detailsList);

In your LWC , just deserialise it using :

const data = JSON.parse(resultString);

Useful links :

8
  • If I just return the json string after desrializing as below, I am getting first level of json but not the second level. In apex : Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(responseBody); Here is what I am trying in js let contentDetails = result['content']; let identityDetails= contentDetails['identity']; result is returned map from apex The value for identityDetails is getting as undefined Commented Mar 3, 2023 at 8:39
  • @user59277 : result['content'] is an array as seen from your question. Try with : contentDetails[0]['identity'] Commented Mar 3, 2023 at 8:59
  • @ 42656 Thanks!!! I am getting the data now but as [object,object]. it seems we retrived only the contenDetails which is at index 0. I have 22 such rows. So should I create an array variable IdentityArray = []; and add all details in a loop iterating it 22 times? Commented Mar 3, 2023 at 9:19
  • No , your content details are present in result['content'] which is an array. result['content'] is your required array. Please accept & upvote if this answer helped you. Welcome. Commented Mar 3, 2023 at 9:29
  • 1
    Or try this in your LWC : const IdentityArray = result['content'].map(ele => ele['identity']); Commented Mar 3, 2023 at 9:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.