Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I really hate 'do this for me' questions but I am at a complete loss. I think I just don't get JSON. So here's an example of my JSON:

"max":"10",
"min":"0",
"attributes":[
    {
        "attributeName":"Fortitude",
        "attributeColor":"#B7B7B7"
    },
    {
        "attributeName":"Vigor",
        "attributeColor":"#D5A6BD"
    },
    {
        "attributeName":"Celerity",
        "attributeColor":"#B4A7D6"
    }
]

It's external and I want to grab it, and then set a js variable to act as an array of attribute objects. So if in JS I set:

 var attributes = [];
 attributes = whatEverNeedsToGoHere;

And then I loop over that variable I could do something like:

console.log(attributes[0].attributeName);

And get "Fortitude". I understand how to get the JSON with jQuery using $.getJSON(); But I don't know get what needs to happen to turn the attributes array into an array of objects.

UPDATE: How I'm calling the JSON in right now.

var attributesData = $.getJSON("jsonDB/attributes.js", function(data){

        var thisAttribute = {"attributeName":String(data[i].attributeName),"attributeColor":String(data[i].attributeColor)};
        attributes.push(thisAttribute);
        console.log(attributes.attributeName);

});
share|improve this question
1  
"It's external and I want to grab it" - Where is your ajax call that does this? – KJ Price Apr 2 at 14:43

3 Answers 3

up vote 1 down vote accepted

This is probably what you are after:

var attributes;

$.getJSON("jsonDB/attributes.js", function(data){
  attributes = data.attributes;
  console.log(attributes);
});

You can test it here. I have stubbed getJSON for this purpose:

function getJSON(url, callbackfn){
  var data = {
    "max":"10",
    "min":"0",
    "attributes":[
      {
        "attributeName":"Fortitude",
        "attributeColor":"#B7B7B7"
      },
      {
        "attributeName":"Vigor",
        "attributeColor":"#D5A6BD"
      },
      {
        "attributeName":"Celerity",
        "attributeColor":"#B4A7D6"
      }
    ]
  }

  callbackfn(data);
}

var attributes;

getJSON("jsonDB/attributes.js", function(data){
  attributes = data.attributes;
  console.log(attributes);
});
share|improve this answer
    
I KNEW IT HAD TO BE THAT SIMPLE. Seamus... I... I love you. – henonChesser Apr 2 at 15:16

Using the open source project jinqJs you could do it like this in one line

var data = {"max":"10",
"min":"0",
"attributes":[
    {
        "attributeName":"Fortitude",
        "attributeColor":"#B7B7B7"
    },
    {
        "attributeName":"Vigor",
        "attributeColor":"#D5A6BD"
    },
    {
        "attributeName":"Celerity",
        "attributeColor":"#B4A7D6"
    }
]
}

var result = jinqJs().from(data).select('attributes');

document.body.innerHTML += '<pre>' + JSON.stringify(result, null, 4) + '</pre>';
<script src="https://rawgit.com/fordth/jinqJs/master/jinqjs.js"></script>

.

var attributes = jinqJs().from('http://..some json url').select('attributes');

This will return a collection of the following:

[
        {
            "attributeName":"Fortitude",
            "attributeColor":"#B7B7B7"
        },
        {
            "attributeName":"Vigor",
            "attributeColor":"#D5A6BD"
        },
        {
            "attributeName":"Celerity",
            "attributeColor":"#B4A7D6"
        }
    ]
share|improve this answer

First of all, your JSon data is invalid.

The proper format for this data being set as Json is the following:

{
    "max": "10",
    "min": "0",
    "attributes": [
        {
            "attributeName": "Fortitude",
            "attributeColor": "#B7B7B7"
        },
        {
            "attributeName": "Vigor",
            "attributeColor": "#D5A6BD"
        },
        {
            "attributeName": "Celerity",
            "attributeColor": "#B4A7D6"
        }
    ]
}

And, in this situation, I do highly advice to use Jspath. Jspath allows you to perform XPath expressions on JSon data.

share|improve this answer
    
I'll look into that, it kind of reminds me of a Query engine for JSON. But is there no native way to do this in JS or jQuery? – henonChesser Apr 2 at 14:54
1  
Seriously an entire library for this small of a JSON packet? – George Jempty Apr 2 at 14:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.