0

I'm trying to parse the response retrieved from a proxied lambda function. I'm using an Ajax call. It fails when I attempt to get the nested Json.

This is the response from the server:

{
    "vehicles": "{\"language\":\"fr-FR\",\"id\":\"10107\",\"make\":\"Panoz\",\"model\":\"Roadster\",\"generation\":\"AIV\",\"yearFrom\":\"1996\",\"yearTo\":\"1999\",\"serie\":\"Roadster\",\"trim\":\"4.6 MT (309 ch)\",\"vehicle\":{\"make\":\"Panoz\",\"model\":\"Roadster\",\"generation\":\"AIV\",\"yearFrom\":\"1996\",\"yearTo\":\"1999\",\"serie\":\"Roadster\",\"trim\":\"4.6 MT (309 ch)\",\"id\":\"10107\",\"bodyWork\":{\"rearTrack\":\"Roadster\",\"width\":\"2\",\"cargoCompartmentVolume\":\"4040\",\"curbWeight\":\"1950\",\"length\":\"1190\",\"numberOfSeater\":\"2655\",\"minTrunkCapacity\":\"1700\",\"maxTrunkCapacityLitre\":\"1620\",\"bodyType\":\"Non disponible\",\"height\":\"Non disponible\",\"fullWeight\":\"Non disponible\",\"cargoCompartment\":\"Non disponible\",\"loadingHeight\":\"1160\",\"frontRearAxleLoad\":\"130\",\"permittedRoadTrainWeight\":\"140\",\"payload\":\"140\",\"frontTrack\":\"Non disponible\",\"wheelbase\":\"Non disponible\",\"groundClearance\":\"Non disponible\"},\"engine\":{\"strokeCycle\":\"Essence\",\"cylinderBore\":\"4601\",\"presenceOfIntercooler\":\"309\",\"boostType\":\"de 5 800\",\"valvesPerCylinder\":\"407\",\"injectionType\":\"Distribué injection\",\"cylinderLayout\":\"V-forme\",\"maximumTorque\":\"8\",\"maxPowerAtRpm\":\"Non disponible\",\"turnoverOfMaximumTorque\":\"90\",\"enginePower\":\"90\",\"capacity\":\"4\",\"engineType\":\"à 4 800\",\"numberOfCylinders\":\"Non disponible\"},\"gearBoxAndHandling\":{\"gearboxType\":\"Manuel\",\"numberOfGear\":\"Arrière\",\"driveWheels\":\"Non disponible\",\"turningCircle\":\"5\"},\"operatingCharacteristics\":{\"cruisingRange\":\"95\",\"fuel\":\"250\",\"emissionStandards\":\"13\",\"fuelTankCapacityLitre\":\"Non disponible\",\"accelerationZeroToHundred\":\"4\",\"maxSpeed\":\"de 330 à 430\",\"cityDrivingFuelConsumptionPer100kmLitre\":\"Non disponible\",\"highwayDrivingFuelConsumptionPer100\":\"43\",\"mixedDrivingFuelConsumptionPer100\":\"10\"},\"suspensionAndBrakes\":{\"rearBrakes\":\"Disques ventilés\",\"frontBrakes\":\"Disques ventilés\",\"backSuspension\":\"Sur bras transversaux\",\"frontSuspension\":\"Barre stabilisatrice\"}},\"dateCreated\":1560071289997,\"dateUpdated\":1560071289997}",
    "language": "fr-FR",
    "id": "10107"
}
var vehicle = JSON.parse(data.vehicles);
                console.log("vehicle: " + vehicle);
                console.log("make: " + vehicle.make);
                console.log("bodyWork: " + vehicle.bodyWork);

The instruction vehicle.make returns the correct value. However when attempting to access the vehicle.bodyWork I receive an undefined answer.

When trying to stringify the data returned, I see the complete correct String. It's as if JSON.parse stopped parsing the first level attributes and couldn't parse the nested objects hence the undefined.

Mikhail Zhuikov
  • 1,213
  • 2
  • 9
  • 19

1 Answers1

2

You're accessing it wrong, like @str said, the value you're looking for is found at vehicle.vehicle.bodyWork. The first vehicle stores the entire object, the second has the nested vehicle object and that has the bodyWork key.

Gaurav Punjabi
  • 153
  • 1
  • 6