7

I have a JSON object which comes back like this from a JavaScript API call:

{
  "myArray": [
    {
      "version": 5,
      "permissionMask": 1
    },
    {
      "version": 126,
      "permissionMask": 1
    }
  ]
}

How can I access the name of the array (i.e myArray) in JavaScript. I need to use the name of the array to determine the flow later on.

5
  • Will there be only one element in the object? Commented May 12, 2015 at 0:43
  • Maybe the keys in lodash is one good option Commented May 12, 2015 at 1:17
  • @thefourtheye There could be any number of elements. I just gave a minified version of the actual json. Commented May 12, 2015 at 12:24
  • @ash123 What should happen if the object has many keys? Commented May 12, 2015 at 12:26
  • thefourtheye- sorry i was wrong. i read it wrong. So , the answer is no, the object has just one element which is an array and that array has many elements. Commented May 13, 2015 at 3:04

3 Answers 3

7

Use getOwnPropertyNames to get a list of the properties of the object in array form.

Example:

var myObj = {
  "myArray": [
    {
      "version": 5,
      "permissionMask": 1

    },
    {
      "version": 126,
      "permissionMask": 1

    }
  ]
},
names = Object.getOwnPropertyNames(myObj);
alert(names[0]); // alerts "myArray"

Note: If the object can have more than one property, like myArray, myInt, and myOtherArray, then you will need to loop over the results of getOwnPropertyNames. You would also need to do type-testing, as in if(names[0] instanceof Array) {...} to check the property type. Based on your example in your question, I have not fleshed all of that out here.

Sign up to request clarification or add additional context in comments.

3 Comments

You assuming it will be first element why? order of the keys is not garanteed
@Luis: "name of the array" in English implies heavily there is only one array. I (and likely Ed Cottrell) do not assume it will be the first element; I do assume it will be the only element (which may or may not be true, but the example data given corroborates the assumption).
Well, in the example, there is only one property. Obviously, if there are more, a loop and some type-testing will be required.
4
Object.keys(data)[0]
# => "myArray"

A terminology note: This solution assumes you have a JavaScript object. You might have a JSON string, in which case this is the solution:

Object.keys(JSON.parse(data))[0]
# => "myArray"

However, "JSON object", in JavaScript, is just one - the one I used just now, that has JSON.parse and JSON.stringify methods. What you have is not a JSON object except perhaps in a trivial interpretation of the second case, where all values in JavaScript are objects, including strings.

3 Comments

Good point re the possibility that this is really a JSON string; the question isn't clear. This is a nice answer.
This did not work for me . When i try to watch myArray in the browser dev console, it shows an unnamed array . So ,it looks like myArray is just a reference to the array.
What do you mean, "watch myArray"? There is no variable called myArray to be watched. My example works (i.e. "accesses the name of the array") if the structure from the OP is in a variable named data. If this is not what you want, please clarify the question.
0

The other answers are good if you have no control over the return format.

However, if you can, I'd recommend changing the return format to put the important values you care about as actual values instead of keys to make it clearer. For example, something like this:

result = 
{
  "name: "myArray",
  "value": [
    {
      "version": 5,
      "permissionMask": 1
    },
    {
      "version": 126,
      "permissionMask": 1
    }
  ]
}

Then, it's a lot clearer to reliably access the property you care about: result.name

1 Comment

I have no control over the data returned , as it is returned by an external api.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.