Soatl
Soatl

Reputation: 10592

JSON object: Get all objects to capitalize their first letters

I have a JSON object that looks something like this (result from an AJAX call):

    json{
      code: 0,
      resultVal: Object {
      data: 
               [
                 Object{
                        generatedName: name1,
                        generatedValue: value1
                 },
                 Object{
                        generatedName1: name2,
                        generatedValue1: value2
                 }....
                ],
        anotherItem: true,
        ...
       }
     }

To clarify resultVal is an object and data is an array of objects, and each object in that array will have two values who's names I will not know in advance.

I am having a problem because I need generatedName and generatedValue to be GenerateName and GeneratedValue. These names and values are usually not the as each other. I know can access each Object through json.resultVal.data[#], but that's as far as I have gotten. json.resultVal.data[0].name returns undefined.

Once I can get those values isolated I can make the fixes I need.

NOTE I am running these calls through Chrome's debugger. The thinking is once I am able to isolate the value I can write the code to fix it using that call. It takes some time to get to this point in the application.

Any suggestions?

Upvotes: 0

Views: 3614

Answers (1)

rjager
rjager

Reputation: 46

If I understood it right, you need to iterate over all keys for all objects in "json.resultVal.data". Try using a for/in loop to iterate over the "data" object, as in:

for( var i in json.resultVal.data ) {
    for( var k in json.resultVal.data[i] ) {
        /* here "k" will be key string ("generatedName", "generatedValue", ...) */
    }
}

Upvotes: 2

Related Questions