Home > Software engineering >  How to get Nested JSON data in effiecient way?
How to get Nested JSON data in effiecient way?

Time:10-23

I have the following JSON data from API, from which I want to get only specific values in an array:

Here is the sample object data:

{
    "matches":[
        {
            "parsedPerson":{
                "personType":"NATURAL",
                "personRole":"PRIMARY",
                "mailingPersonRoles":[
                    "ADDRESSEE"
                ],
                "gender":{
                    "gender":"MALE",
                    "confidence":0.9325842696629214
                },
                "addressingGivenName":"Brandyn",
                "addressingSurname":"Kramer",
                "outputPersonName":{
                    "terms":[
                        {
                            "string":"Brandyn",
                            "termType":"GIVENNAME"
                        },
                        {
                            "string":"Kramer",
                            "termType":"SURNAME"
                        }
                    ]
                }
            },
            "parserDisputes":[
                
            ],
            "likeliness":0.9427026792672768,
            "confidence":0.9433333333333334
        }
    ],
    "bestMatch":{
        "parsedPerson":{
            "personType":"NATURAL",
            "personRole":"PRIMARY",
            "mailingPersonRoles":[
                "ADDRESSEE"
            ],
            "gender":{
                "gender":"MALE",
                "confidence":0.9325842696629214
            },
            "addressingGivenName":"Brandyn",
            "addressingSurname":"Kramer",
            "outputPersonName":{
                "terms":[
                    {
                        "string":"Brandyn",
                        "termType":"GIVENNAME"
                    },
                    {
                        "string":"Kramer",
                        "termType":"SURNAME"
                    }
                ]
            }
        },
        "parserDisputes":[
            
        ],
        "likeliness":0.9427026792672768,
        "confidence":0.9433333333333334
    }
}

For example, I want to get 'string' value where

 'termType=GIVENNAME' (Tom,Henry) 
 'termType=SURNAME'   (Smith)     
  and 'gender'        (MALE)      

As a beginner, if I want to get 'gender', I will do the following:

var gender = data.bestMatch.parsedPerson.gender.gender

but I do not think this is the right way to approach this, I want to get all the data in an array using a loop, something like this, or may be more better way:

var Given = [Tom,Henry] 
var Surname = [Smith]   
var gender = [MALE]     

I am trying to use following code snippet to get values where termType = GIVENNAME but it gives undefined as an output:

const findName = (obj, key) => {
   const arr = obj['outputPersonName'];
   if(arr.length){
      const result = arr.filter(el => {
         return el['termType'] === key;
      });
      if(result && result.length){
         return result.string;
      }
      else{
         return '';
      }
   }
}
console.log(findName(data.parsedPerson, 'GIVENNAME'));

Any guidance would be much appreciated.

CodePudding user response:

Here is how you can get Surname, Givenname, and Gender from this JSON data:

     var Name = {
     "context" : {
     "priority" : "REALTIME",
     "properties" : [ ]
     },
     "inputPerson" : {
     "type" : "NaturalInputPerson",
     "personName" : {
     "nameFields" : [ {
     "string" : "Sample Name",
     "fieldType" : "FULLNAME"
      }]
    },
    "gender" : "UNKNOWN"
  }


  var options = {
 'method' : 'post',
 'contentType': 'application/json',
 'payload' : JSON.stringify(Name),
 
};


   var response = UrlFetchApp.fetch(url,options).getContentText();
   var data = JSON.parse(response);
   var gender =  data.bestMatch.parsedPerson.gender.gender;
   var Given =   data.bestMatch.parsedPerson.addressingGivenName; 
   var Surname = data.bestMatch.parsedPerson.addressingSurname;

Try it out and let me know if it worked for you.

  • Related