bbk bbk - 4 months ago 14
Javascript Question

JSON object to form a HTML table

I need to extract values from the below JSON response I am receiving from a web service response.

{"ns4:SearchSA_PartyReturn": {
"xmlns:ns1": "urn:cs-base",
"xsi:type": "ns4:SearchSA_PartyReturn",
"ns4:object": {
"xmlns:ns2": "urn:co-base",
"recordCount": 3,
"xmlns:ns0": "urn:cs-rest",
"ns3:item": [
{
"ns3:SA_Party": {
"ns3:partyName": "Akorn New Jersey Inc",
"ns3:partyStatus": "ACTIVE",
"ns2:rowidObject": 20011,
"ns3:SAC_Address": {
"ns3:item": {
"ns3:city": "QUE",
"ns3:state": "N",
"ns2:rowidObject": 20011,
"ns3:postalCode": -4,
"ns3:addressType": "P",
"ns3:country": "US",
"ns3:addressLine": "RD"
},
"pageSize": 10,
"firstRecord": 1,
"searchToken": "SVR1.TD3V"
},
"ns3:partyType": "Pharma",
"ns3:SAC_Person": {
"ns3:item": {
"ns3:firstName": "DO",
"ns2:rowidObject": 11,
"ns3:lastName": "MA",
"ns3:middleName": "R",
"ns3:personType": 2
},
"pageSize": 10,
"firstRecord": 1,
"searchToken": "SVR1.TD3U"
}
},
"ns3:changeSummary": {
"logging": false,
"xmlns:sdo": "commonj.sdo"
}
},
{
"ns3:SA_Party": {
"ns3:partyName": "Akorn New Jersey Inc",
"ns3:partyStatus": "ACTIVE",
"ns2:rowidObject": 20047,
"ns3:SAC_Address": {
"ns3:item": {
"ns3:city": "SC",
"ns3:state": "N",
"ns2:rowidObject": 20047,
"ns3:postalCode": 12,
"ns3:addressType": "B",
"ns3:country": "US",
"ns3:addressLine": "OTT STET"
},
"pageSize": 10,
"firstRecord": 1,
"searchToken": "SVR1.TD3X"
},
"ns3:partyType": "Pharma",
"ns3:SAC_Person": {
"ns3:item": {
"ns3:firstName": "GE",
"ns2:rowidObject": 47,
"ns3:lastName": "HA",
"ns3:middleName": "B",
"ns3:personType": 2
},
"pageSize": 10,
"firstRecord": 1,
"searchToken": "SVR1.TD3W"
}
},
"ns3:changeSummary": {
"logging": false,
"xmlns:sdo": "commonj.sdo"
}
},
{
"ns3:SA_Party": {
"ns3:partyName": "Cig Na Ltd",
"ns3:partyStatus": "ACTIVE",
"ns2:rowidObject": 20040,
"ns3:SAC_Address": {
"ns3:item": {
"ns3:city": "JA",
"ns3:state": "NY",
"ns2:rowidObject": 20040,
"ns3:postalCode": 1,
"ns3:addressType": "P",
"ns3:country": "US",
"ns3:addressLine": "QUR"
},
"pageSize": 10,
"firstRecord": 1,
"searchToken": "SVR1.TD3Z"
},
"ns3:partyType": "Insurance",
"ns3:SAC_Person": {
"ns3:item": {
"ns3:firstName": "A",
"ns2:rowidObject": 40,
"ns3:lastName": "Q",
"ns3:personType": 2
},
"pageSize": 10,
"firstRecord": 1,
"searchToken": "SVR1.TD3Y"
}
},
"ns3:changeSummary": {
"logging": false,
"xmlns:sdo": "commonj.sdo"
}
}
],
"pageSize": 10,
"firstRecord": 1
},
"xmlns:ns3": "urn:co-ors",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xmlns:ns4": "urn:cs-ors"
}}


Can anyone help me with a javascript/jquery to extract
ns3:SA_Party
with its corresponding children
ns3:SAC_Address
and
ns3:SAC_Person
. (SA_Party is the parent with two children SAC_Address and SAC_Person). I need the underlying attribute, too.

Answer

Assume the data variable is a valid JSON object that contains your response

var items = data['ns4:SearchSA_PartyReturn']['ns4:object']['ns3:item'];

var mapped = items.map(function(item){ 
  return {
    'SAC_Address':item['ns3:SA_Party']['ns3:SAC_Address'], 
    'SAC_Person':item['ns3:SA_Party']['ns3:SAC_Person']
  }; 
});

console.log(mapped);

The mapped variable will be an array with Objects containing the SAC_Address and SAC_Person info.

Hopefully this helps.