Ali Celebi
Ali Celebi

Reputation: 1008

How to parse a JSON document and extract an array?

The code below produces an output as shown below which is wrong as per my expectation.

Wrong format

The format I expect to have is shown below. How can I achieve this? What am I doing wrong?

I try and drill-down the structure to get a list of records from dealerListItemDToes array however I can't figure out a way to have it in the structure below.

CorrectFormat

let responseData = {
  "_embedded": {
    "dealerListItemDToes": [
      {
        "id": "ab4b23bb-b58d-4012-93ea-702e37ea555a",
        "gssnID": "GSM00000001",
        "companyID": "GCM00000001",
        "mbID": "41010f",
        "name": "GINO RAG. FELICE & FIGLIO - S.p.A.",
        "website": "http://www.gino.mercedes-benz.it",
        "phone": "+39 0171 410777",
        "country": "Italy",
        "city": "Madonna",
        "postalCode": "12100",
        "address": "Madonna dellolmo",
        "description": "description",
        "createdBy": "DataAdmin",
        "updatedBy": "DataAdmin",
        "createdAt": null,
        "updatedAt": null,
        "business": "PVU",
        "passengerCar": true,
        "van": true,
        "bus": false,
        "unimog": true,
        "truck": false
      },
      {
        "id": "b644a324-6858-4ac9-a2ce-a4539cee43a5",
        "gssnID": "GSM00000002",
        "companyID": "GCM00000002",
        "mbID": "42010e",
        "name": "L-AUTO. S.r.l.",
        "website": "http://www.lauto.mercedes-benz.it",
        "phone": "+39 0775 8859500",
        "country": "Italy",
        "city": "Frosinone",
        "postalCode": "3100",
        "address": "Via Enrico Fermi, 25",
        "description": "description",
        "createdBy": "DataAdmin",
        "updatedBy": "DataAdmin",
        "createdAt": null,
        "updatedAt": null,
        "business": "PTBU",
        "passengerCar": true,
        "van": false,
        "bus": true,
        "unimog": true,
        "truck": true
      }
    ]
  },
  "_links": {
    "first": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=0&size=10&sort=gssnID,asc"
    },
    "self": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=0&size=10&sort=gssnID,asc"
    },
    "next": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=1&size=10&sort=gssnID,asc"
    },
    "last": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=2&size=10&sort=gssnID,asc"
    }
  },
  "page": {
    "size": 10,
    "totalElements": 27,
    "totalPages": 3,
    "number": 0
  }
};

function getDataRecords(providedData: any): Array<any> {
  const dataSet: Array<any> = Object.entries(providedData._embedded)[0];
  dataSet.splice(0, 1); // Delete first property as its name can vary
  return dataSet;
}

console.log(getDataRecords(responseData));

Upvotes: 1

Views: 65

Answers (1)

Lajos Arpad
Lajos Arpad

Reputation: 76444

I have converted your typescript into a Javascript code like

let responseData = {
  "_embedded": {
    "dealerListItemDToes": [
      {
        "id": "ab4b23bb-b58d-4012-93ea-702e37ea555a",
        "gssnID": "GSM00000001",
        "companyID": "GCM00000001",
        "mbID": "41010f",
        "name": "GINO RAG. FELICE & FIGLIO - S.p.A.",
        "website": "http://www.gino.mercedes-benz.it",
        "phone": "+39 0171 410777",
        "country": "Italy",
        "city": "Madonna",
        "postalCode": "12100",
        "address": "Madonna dellolmo",
        "description": "description",
        "createdBy": "DataAdmin",
        "updatedBy": "DataAdmin",
        "createdAt": null,
        "updatedAt": null,
        "business": "PVU",
        "passengerCar": true,
        "van": true,
        "bus": false,
        "unimog": true,
        "truck": false
      },
      {
        "id": "b644a324-6858-4ac9-a2ce-a4539cee43a5",
        "gssnID": "GSM00000002",
        "companyID": "GCM00000002",
        "mbID": "42010e",
        "name": "L-AUTO. S.r.l.",
        "website": "http://www.lauto.mercedes-benz.it",
        "phone": "+39 0775 8859500",
        "country": "Italy",
        "city": "Frosinone",
        "postalCode": "3100",
        "address": "Via Enrico Fermi, 25",
        "description": "description",
        "createdBy": "DataAdmin",
        "updatedBy": "DataAdmin",
        "createdAt": null,
        "updatedAt": null,
        "business": "PTBU",
        "passengerCar": true,
        "van": false,
        "bus": true,
        "unimog": true,
        "truck": true
      }
    ]
  },
  "_links": {
    "first": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=0&size=10&sort=gssnID,asc"
    },
    "self": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=0&size=10&sort=gssnID,asc"
    },
    "next": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=1&size=10&sort=gssnID,asc"
    },
    "last": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=2&size=10&sort=gssnID,asc"
    }
  },
  "page": {
    "size": 10,
    "totalElements": 27,
    "totalPages": 3,
    "number": 0
  }
};

function getDataRecords(providedData) {
  const dataSet = Object.entries(providedData._embedded)[0];
  dataSet.splice(0, 1); // Delete first property as its name can vary
  return dataSet;
}

console.log(getDataRecords(responseData));

This results in

enter image description here

and the only problem is that the array that you expect is the first and only element of another array, wrapped around it. So, I solved it like this:

let responseData = {
  "_embedded": {
    "dealerListItemDToes": [
      {
        "id": "ab4b23bb-b58d-4012-93ea-702e37ea555a",
        "gssnID": "GSM00000001",
        "companyID": "GCM00000001",
        "mbID": "41010f",
        "name": "GINO RAG. FELICE & FIGLIO - S.p.A.",
        "website": "http://www.gino.mercedes-benz.it",
        "phone": "+39 0171 410777",
        "country": "Italy",
        "city": "Madonna",
        "postalCode": "12100",
        "address": "Madonna dellolmo",
        "description": "description",
        "createdBy": "DataAdmin",
        "updatedBy": "DataAdmin",
        "createdAt": null,
        "updatedAt": null,
        "business": "PVU",
        "passengerCar": true,
        "van": true,
        "bus": false,
        "unimog": true,
        "truck": false
      },
      {
        "id": "b644a324-6858-4ac9-a2ce-a4539cee43a5",
        "gssnID": "GSM00000002",
        "companyID": "GCM00000002",
        "mbID": "42010e",
        "name": "L-AUTO. S.r.l.",
        "website": "http://www.lauto.mercedes-benz.it",
        "phone": "+39 0775 8859500",
        "country": "Italy",
        "city": "Frosinone",
        "postalCode": "3100",
        "address": "Via Enrico Fermi, 25",
        "description": "description",
        "createdBy": "DataAdmin",
        "updatedBy": "DataAdmin",
        "createdAt": null,
        "updatedAt": null,
        "business": "PTBU",
        "passengerCar": true,
        "van": false,
        "bus": true,
        "unimog": true,
        "truck": true
      }
    ]
  },
  "_links": {
    "first": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=0&size=10&sort=gssnID,asc"
    },
    "self": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=0&size=10&sort=gssnID,asc"
    },
    "next": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=1&size=10&sort=gssnID,asc"
    },
    "last": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=2&size=10&sort=gssnID,asc"
    }
  },
  "page": {
    "size": 10,
    "totalElements": 27,
    "totalPages": 3,
    "number": 0
  }
};

function getDataRecords(providedData) {
  const dataSet = Object.entries(providedData._embedded)[0];
  dataSet.splice(0, 1); // Delete first property as its name can vary
  return dataSet[0];
}

console.log(getDataRecords(responseData));

Resulting in

enter image description here

If this solves the problem, then the only modification on your almost correct script is that on the return statement I have specified to return the 0th element of the outer array, which is the result that you expect.

Upvotes: 1

Related Questions