Edward
Edward

Reputation: 25

How to display a property from JSON data

I want to display or console log the values of something in this object. For example the health property.

{
    health: 1000,
    neonColor: [
        255,
        0,
        255
    ],
    modSideSkirt: -1,
    modHood: -1,
    // and more
}

I'm using JavaScript and have tried everything.

Upvotes: 0

Views: 942

Answers (2)

Krutik Raut
Krutik Raut

Reputation: 139

let data = 
{
  health: 1000,
  neonColor: [255, 0, 255],
  modSideSkirt: -1,
  modHood: -1,
  modFender: -1,
  modHorns: -1,
  modAerials: -1,
  windowTint: -1,
  extras: { "12": false, "11": true },
  modTransmission: -1,
  modAPlate: -1,
  windows: [1, 1, 1, false, false, false, 1, 1, 1, 1, 1, 1, false],
  modDoorSpeaker: -1,
  modFrame: -1,
  neonEnabled: [false, false, false, false],
  dirtLevel: 3.0178611278534,
  modArchCover: -1,
  modSuspension: -1,
  modPlateHolder: -1,
  model: -511601230,
  modExhaust: -1,
  modGrille: -1,
  modSpoilers: -1,
  modEngine: 1,
  modXenon: false,
  modStruts: -1,
  plate: "LVQ 594",
  modRearBumper: -1,
  modAirFilter: -1,
  color1: 1,
  modSteeringWheel: -1,
  modTurbo: false,
  modDial: -1,
  modRightFender: -1,
  modHydrolic: -1,
  modOrnaments: -1,
  modSpeakers: -1,
  pearlescentColor: 10,
  modWindows: -1,
  fuelLevel: 77.161033630371,
  modEngineBlock: -1,
  tyreSmokeColor: [255, 255, 255],
  modDashboard: -1,
  bodyHealth: 1000.0,
  doors: [false, false, false, false, false, false],
  modTrimB: -1,
  modSeats: -1,
  color2: 1,
  modTank: -1,
  modTrunk: -1,
  modBrakes: -1,
  wheels: 0,
  modVanityPlate: -1,
  wheelColor: 156,
  modArmor: -1,
  modBackWheels: -1,
  modSmokeEnabled: 1,
  modFrontWheels: -1,
  modRoof: -1,
  plateIndex: 0,
  modShifterLeavers: -1,
  modLivery: -1,
  engineHealth: 1000.0,
  modFrontBumper: -1,
  modTrimA: -1,
  tyres: [false, false, false, false, false, false, false],
};
let health = data["health"];
console.log("Health:",health);

Upvotes: 0

mikayelgrigoryan
mikayelgrigoryan

Reputation: 563

You can just do it like this

const data = {
    // All the data goes here
};
console.log(data.health);

Or perhaps if you have the stringified version of that JSON object you could try doing

const data = {
    // All the data goes here
};
const foo = JSON.parse(data);
console.log(foo.health);

Upvotes: 1

Related Questions