views:

25

answers:

1

Hi, I am trying to parse a JSON output:

http://www.freebase.com/experimental/topic/standard?id=/en/colonel_sanders

I'd like to put the basic data into an array using Javascript. In the "properties" object I'd like to grab any "text" element one level under "properties" as a label and grab the "text" under the "values" object to match the label.

For the above I would get:

  • "description": "Harland David Sanders, better known as Colonel Sanders...
  • "Organizations founded": KFC
  • "Cause of death": Leukemia
  • "Date of death": Dec 16, 1980
  • "Place of death": Louisville
  • "Date of birth": Sep 9, 1890
  • "Gender": Male

etc...

I have some code which recursively runs through the JSON but I am a novice with javascript and JSON and am having a lot trouble in step one:

Firstly, grabbing the "text" trying by identifying an element as being "an element of" the main properties object; then

Secondly grabbing from the associated values array any text element (if the value is a collection then I would like to concatenate the strings from the text or otherwise ignore it).

I hope that make sense.

nb. the code I use is similar to here: http://tlrobinson.net/projects/javascript-fun/jsondiff/

+1  A: 

This should get you started:

<script>
  function cb(response) {
    var props = {};
    var properties = response['/en/colonel_sanders'].result.properties;
    for (var p_id in properties) {
      var prop = properties[p_id];
      props[prop.text]=prop.values[0].text;
    }
    console.log(props);
  }
</script>
<script src="http://www.freebase.com/experimental/topic/standard?id=/en/colonel_sanders&amp;callback=cb"&gt;&lt;/script&gt;
Will Moffat