-3

EDIT: Going off teemu's comment below, this is similar to his recommended post, but none of my objects have arrays inside of them, so that post did not solve my problem.

EDIT: I cannot just do

data2.forEach(function(provider) {provider['parameters']['name']});

because there is only one object with the key of 'name'. The function needs to be iterative and able to extract all the information from every object at once

EDIT: I have parsed all the JSON now I am just trying to write a function/method which extracts the part that I want. The "data2" below is the variable which stores the parsed JSON

I have an API sending me unorganized JSON that I need to extract certain parts of. Below you see the "parameters" object. I need to extract everything within it. For example I need to extract "nameInfo": "name" and "lastName", "alarmSet": "time" and "temperature", "business": "businessName" and "yearsInBusiness", etc.

Goal: I am trying to make a table that says nameInfo bob smith, alarmSet 5 o'clock 72 degrees, etc. etc.

The only thing that all of the JSON has in common is the "parameters" title. Everything else inside the "parameters" object is different. And everything within the objects inside the "parameters" object is different.

Currently what I'm doing is...

data2.forEach(function(provider) {provider['parameters']});

and this just returns [object Object] [object Object] [object Object] [object Object] [object Object] [object Object] etc....

"paramaters": {
    "info": {
        "name": "bob",
        "lastName": "smith"
    }
     "alarmSet": {
        "time": "5 o'clock"
        "temperature": "72 degrees"
    }
}   

"parameters": {
     "business": {
        "businessName": "ice cream shop",
        "yearsInBusiness": "17 years"
     }
         "policeRecords": {
         "misdemeanors": "attempted ice cream stealing",
        "felonies": "convicted of melting ice cream"
    }
}   
AnonUser
  • 19
  • 7
  • @Teemu Very close, but I do not have arrays inside of my JSON :/ I will continue to read this post and see if it will help me. – AnonUser Jun 17 '16 at 20:15
  • @Teemu If you could please point me to where that post answers my question I would greatly appreciate it. I feel people are reading your comment and assuming it is the same post, when it is not. Thank you! – AnonUser Jun 17 '16 at 20:21
  • @AnonUser It doesn't matter if you have any arrays in your code. That post teaches you how to access data in objects **and** arrays. – Mike Cluck Jun 17 '16 at 20:25
  • @MikeC I read through all of it and it did not teach me how to retrieve the information I asked to retrieve above. I am dealing with an object inside an object which is inside an object. If you could please point to where it teaches me what I kindly requested, I would appreciate it so very much! Thank you! – AnonUser Jun 17 '16 at 20:27
  • @AnonUser It most certainly does. Assuming your object is stored in a variable called `data`, you can access those values through: `data.parameters.info.name`, `data.parameters.info.lastName`, `data.parameters.alarmSet.time`, etc. – Mike Cluck Jun 17 '16 at 20:32
  • Not at all clear what your specific problem is either. See: [ask] – charlietfl Jun 17 '16 at 20:34
  • @charlietfl I will edit it now to try to make it more clear – AnonUser Jun 17 '16 at 20:34
  • @MikeC I asked my question incorrectly, I forgot to mention that It needs to be recursive, as you see in the method it says forEach. I will edit it and mention that, thank you for telling me – AnonUser Jun 17 '16 at 20:36
  • @AnonUser Using `forEach` does not mean it's recursive. If anything, it means it's iterative. It also doesn't change how you access data properties. – Mike Cluck Jun 17 '16 at 20:38
  • @MikeC As you probably noticed I am a beginner with programming, I apologize for my inability to reference things correctly. I am trying to access them all at once, the JSON is many many pages. – AnonUser Jun 17 '16 at 20:39
  • @teemu Still no proof of my question being answered in that post. Please help me – AnonUser Jun 17 '16 at 20:51
  • @MikeC Still no proof of my question being answered in that post. Please help me – AnonUser Jun 17 '16 at 20:52
  • @naomik no proof of my question being answered in that post. Please help me – AnonUser Jun 17 '16 at 20:52
  • @charlietfl no proof of my question being answered in that post. Please help me – AnonUser Jun 17 '16 at 20:53
  • 1
    But this isn't a tutorial service to teach you how to understand objects. The linked answer doe explain many issues regarding how to access them – charlietfl Jun 17 '16 at 21:46
  • 1
    In the accepted answer for the linked question, the section titled **Accessing nested data structures** precisely answers what you're asking. That is fundamental to nested data structures in Javascript, regardless of whether the data originated as JSON or from another source. – Michael Gaskill Jun 18 '16 at 01:26

1 Answers1

0

So if I am understanding you correctly you are wanting to retrieve a dynamic object inside an object? If this is the case then you need to parse your JSON into a usable Javascript object.

Ex:

var parsedData = JSON.parse(data2);
parsedData.forEach(function(o, i){
       //o is now your full dynamic object for instance you can do o.business.businessName

});
Ohjay44
  • 897
  • 7
  • 20