0

I am a beginner and, as I began to increase the complexity of JSON, I started to confuse myself by accessing it. How do I access the following JSON? I would like to access the value and keys of the services

{
  employers:{
    Mike: {
      old: 20,   
      services:{
        cut : {
          value : 10
        }
        hair_straightening : {
            value: 20
        }
      }
    }
    Penny: {
      old: 20,   
      services:{
        cut : {
          value : 10
        }
        hair_straightening : {
            value: 20
        }
      }
    }
  }
}
Isaac Meneses
  • 163
  • 1
  • 12
  • Why aren't you using array to store `employes` and their `services`? It will be much easier to loop through your data! – ibrahim mahrir Feb 09 '17 at 21:56
  • 2
    Well, for starters, this is not JSON, this is what's affectionately called a POJO (Plain Old Javascript Object). To get the list of keys for an object, you can call `Object.keys(myObj);` and to access property values, they can be accessed statically through dot (`.`) syntax, or bracket (`[]`) syntax – mhodges Feb 09 '17 at 22:02
  • @mhodges I think `POJO` should be a standard for that. I've been seeing a lot of people calling a POJO, JSON. – ibrahim mahrir Feb 09 '17 at 22:05

3 Answers3

2

Everyone has to start somewhere :)

I'm not sure if I completely understand what you're after, but here goes...

First off, it looks like your notation on your object is subtly off when listing your keys. For example, between Mike and Penny you should have a comma separating each of those keys in the larger employers object.

Something like:

employers: {
  Mike: {
         ...
    }, //need a comma here
    Penny: {
       ...
    }
}

Also, within each of those employers, there should be a comma between the keys for cut and hair_straightening.

        services: {
            cut: {
                value: 10
            }, //need a comma here
            hair_straightening: {
                value: 20
            }
        }

Now to your actual question... To get the keys for each of the services, you can use Object.keys(). This function would get you they keys for a given employer. Then you can also grab the values from inside that same function. (Note this is for only one employer; you'd just want to iterate over both and use this same function on each)

function getServices(employer) {
  var services = employer.services;
  var servicesKeys = Object.keys(services);
  var serviceValueMatrix = [];
  servicesKeys.forEach(function(service) {
    serviceValueMatrix.push([service, employer.services[service].value])
  })
  return serviceValueMatrix;
}
// assuming you had var yourJSON = { employers: {...} }
// getServices(yourJSON.employers.Mike);
// returns [["cut",10], ["hair_straightening",20]]

Also, given that your JSON object is already in key:value format, you could probably skip the last set of objects in the format of value: 10, value: 20 etc, and instead just make the last tier of your object something like:

services:{
  cut: 10,
  hair_straightening: 20
}

Then you could just grab services.cut and services.hair_straightening.

Full code below for clarity:

const yourJSON = {
  employers: {
      Mike: {
          old: 20,
          services: {
              cut: {
                  value: 10
              },
              hair_straightening: {
                  value: 20
              }
          }
      },
      Penny: {
          old: 20,
          services: {
              cut: {
                  value: 10
              },
              hair_straightening: {
                  value: 20
              }
          }
      }
   }
}

function getServices(employer) {
  var services = employer.services;
  var servicesKeys = Object.keys(services);
  var serviceValueMatrix = [];
  servicesKeys.forEach(function(service) {
    serviceValueMatrix.push([service, employer.services[service].value])
  })
  return serviceValueMatrix;
}

console.log(getServices(yourJSON.employers.Mike));
// returns [["cut",10], ["hair_straightening",20]]
1

your json should look something like this:

{ employers:[ { name: "Mike", age: 20, services: [ {name:"cut",value:10}, {name:"hair_straightening",value:20} ]
},{ name: "Penny", age: 20, services: [ {name:"cut",value:10}, {name:"hair_straightening",value:20} ]
} ] }

circa94
  • 73
  • 4
  • how can I acess the services in this format? – Isaac Meneses Feb 09 '17 at 22:22
  • assign this json to a variable like var json = yourjson then very simple access: json.employers[0].services will give you the services from mike you could also iterate through json.employers like in the answer from shadymoses – circa94 Feb 09 '17 at 22:26
0

You can use Object.keys to get the keys of an object as an array, then you can loop through that nicely.

// in this case json is a variable representing your parsed data
Object.keys(json).map(function(key) {
  console.log(json[key])
  return json[key].services
})

That would give you an array of services objects.

shadymoses
  • 3,273
  • 1
  • 19
  • 21