Home > front end >  Convert incoming JSON string to Javascript array with ability to GroupBy
Convert incoming JSON string to Javascript array with ability to GroupBy

Time:03-12

I have an API which returns a JSON, like:

{
"CaseAssgnedDt": "2011-07-22T17:12:19",
"InquiryId": "08-CA-036441",
"DisputeUnitCity": "Cleveland",
"CaseClasification": "Unclassified",
"CaseClosedDt": null,
"Created": "2010-10-07T15:32:29",
"CaseDescription": null,
"LastUpd": "2019-10-20T17:17:14",
"LastUpdBy": "1-CGA",
"CaseName": "Sample test case name",
"CaseNumber": "08-CA-036441",
"CaseSource": "Visit",
"DisputeUnitState": "OH",
"CaseStatus": "Open",
"CaseSubType": "CA",
"CaseSubTypeCd": null,
"CaseType": "C",
"CaseLongName": "Long sample test case name",
"Num8a3Discriminatees": null,
"Num8b2Discriminatees": null,
"NumOfEmployees": 146,
"PostElectionSelfCertification": null,
"Potential10j": "N",
"RegionRecommendsPursuing10j": "N",
}

I am retrieving that from the API in Javascript using the Fetch API.

Once I retrieve it, I wish to convert it into a Javascript array of Name/Value pairs, matching the specific format as follows:

[
{fieldName:"CaseAssgnedDt", fieldValue: "2011-07-22T17:12:19"},
{fieldName: "InquiryId", fieldValue: "08-CA-036441"},
{fieldName: "DisputeUnitCity", fieldValue: "Cleveland"},
{fieldName: "CaseClasification", fieldValue: "Unclassified"},
{fieldName: "CaseClosedDt", fieldValue: null},
{fieldName: "Created", fieldValue: "2010-10-07T15:32:29"},
{fieldName: "CaseDescription", fieldValue: null},
{fieldName: "LastUpd", fieldValue: "2019-10-20T17:17:14"},
{fieldName: "LastUpdBy", fieldValue: "1-CGA"},
{fieldName: "CaseName", fieldValue: "Sample test case name"},
{fieldName: "CaseNumber", fieldValue: "08-CA-036441"},
{fieldName: "CaseSource", fieldValue: "Visit"},
{fieldName: "DisputeUnitState", fieldValue: "OH"},
{fieldName: "CaseStatus", fieldValue: "Open"},
{fieldName: "CaseSubType", fieldValue: "CA"},
{fieldName: "CaseSubTypeCd", fieldValue: null},
{fieldName: "CaseType", fieldValue: "C"},
{fieldName: "CaseLongName", fieldValue: "Long sample test case name"},
{fieldName: "Num8a3Discriminatees", fieldValue: null},
{fieldName: "Num8b2Discriminatees", fieldValue: null},
{fieldName: "NumOfEmployees", fieldValue: 146},
{fieldName: "PostElectionSelfCertification", fieldValue: null},
{fieldName: "Potential10j", fieldValue: "N"},
{fieldName: "RegionRecommendsPursuing10j", fieldValue: "N"}
]

So essentially, I want the equivalent of:

const caseData = [
{fieldName:"CaseAssgnedDt", fieldValue: "2011-07-22T17:12:19"},
{fieldName: "InquiryId", fieldValue: "08-CA-036441"},
{fieldName: "DisputeUnitCity", fieldValue: "Cleveland"},
{fieldName: "CaseClasification", fieldValue: "Unclassified"},
{fieldName: "CaseClosedDt", fieldValue: null},
{fieldName: "Created", fieldValue: "2010-10-07T15:32:29"},
{fieldName: "CaseDescription", fieldValue: null},
{fieldName: "LastUpd", fieldValue: "2019-10-20T17:17:14"},
{fieldName: "LastUpdBy", fieldValue: "1-CGA"},
{fieldName: "CaseName", fieldValue: "Sample test case name"},
{fieldName: "CaseNumber", fieldValue: "08-CA-036441"},
{fieldName: "CaseSource", fieldValue: "Visit"},
{fieldName: "DisputeUnitState", fieldValue: "OH"},
{fieldName: "CaseStatus", fieldValue: "Open"},
{fieldName: "CaseSubType", fieldValue: "CA"},
{fieldName: "CaseSubTypeCd", fieldValue: null},
{fieldName: "CaseType", fieldValue: "C"},
{fieldName: "CaseLongName", fieldValue: "Long sample test case name"},
{fieldName: "Num8a3Discriminatees", fieldValue: null},
{fieldName: "Num8b2Discriminatees", fieldValue: null},
{fieldName: "NumOfEmployees", fieldValue: 146},
{fieldName: "PostElectionSelfCertification", fieldValue: null},
{fieldName: "Potential10j", fieldValue: "N"},
{fieldName: "RegionRecommendsPursuing10j", fieldValue: "N"}
]

Except instead of coding that array, I want to dynamically build it from the JSON retrieved.

This is all, ultimately, to get my data in a format so that I can implement code similar to that referenced in https://docs.microsoft.com/en-us/office/dev/add-ins/concepts/correlated-objects-pattern, under the "Process objects in the document with the correlated objects pattern" section.

Please provide code to illustrate or demonstrate your solution.

Thank you.

CodePudding user response:

you can use map on the Object.entries

let x = {
"CaseAssgnedDt": "2011-07-22T17:12:19",
"InquiryId": "08-CA-036441",
"DisputeUnitCity": "Cleveland",
"CaseClasification": "Unclassified",
"CaseClosedDt": null,
"Created": "2010-10-07T15:32:29",
"CaseDescription": null,
"LastUpd": "2019-10-20T17:17:14",
"LastUpdBy": "1-CGA",
"CaseName": "Sample test case name",
"CaseNumber": "08-CA-036441",
"CaseSource": "Visit",
"DisputeUnitState": "OH",
"CaseStatus": "Open",
"CaseSubType": "CA",
"CaseSubTypeCd": null,
"CaseType": "C",
"CaseLongName": "Long sample test case name",
"Num8a3Discriminatees": null,
"Num8b2Discriminatees": null,
"NumOfEmployees": 146,
"PostElectionSelfCertification": null,
"Potential10j": "N",
"RegionRecommendsPursuing10j": "N",
}

let res = Object.entries(x).map(([k,v])=>{
    return {fieldName:k, value: v}
})
console.log(res)

  • Related