0

I get JSON object from Mongo DB. This is JSON.

**JSON**
{
    "_id" : ObjectId("5265347d144bed4968a9629c"),
    "name" : "ttt",
    "features" : {
        "t" : {
            "visual_feature" : "t",
            "type_feature" : "Numeric",
            "description_feature" : "Time"
        },
        "y" : {
            "visual_feature" : "y",
            "type_feature" : "Nominal",
            "description_feature" : "Values to be mapped to the y-axis"
        },
        "x" : {
            "visual_feature" : "x",
            "type_feature" : "Numeric",
            "description_feature" : "Values to be mapped to the x-axis"
        }
    }
}

I'm trying to build a table from "features" attributes in JSON object. How to access to "features" attributes (it is a sub json object)in javascript? It is important to get values from "visual_feature", "type_feature" and "description_feature". UPD I have a solution.

  $.ajax({
                                url: VASERVER_API_LOC + '/visualization/' + visid + '/',
                                type: 'GET',
                                contentType: "application/json",
                                 data: tmp_object,
                                success: function(json) {   
                                    var result = [];                         
                                    var keys = Object.keys(json);
                                    keys.forEach(function (key){
                                    result.push(json[key]);
                                    });

                                    for(var i=0; i<result.length; i++){
                                    console.log(">>>  visual_feature  ==  " + result[i].visual_feature);
                                    console.log(">>> type_feature  ==   "  + result[i].type_feature);
                                    console.log(">>>  discription_feature  ==  " + result[i].description_feature);
                                    };

                                }
                            });

Thank you!!!

5
  • 5
    So... what have you tried so far? It's incredibly lazy to dump a set of requirements here waiting for someone to write code for you. Commented Oct 21, 2013 at 14:43
  • for (feature in data.features) console.log(features[feature].visual_feature) Commented Oct 21, 2013 at 14:45
  • 1
    they should create a subdomain: simplistic-json-questions.stackoverflow.com -- there are literally hundreds of these questions out there Commented Oct 21, 2013 at 14:49
  • thank you. I have a own solutin. Commented Oct 21, 2013 at 15:04
  • You can replace your forEach() / push() with map(). Commented Oct 21, 2013 at 16:24

2 Answers 2

2

Assuming your JSON result is an object, loop through like this:

for (var feature in result.features) {
  if (object.hasOwnProperty(feature)) {
    // do table building stuff
    console.log(feature);
  }
}

If it's not an object, you'll do JSON.parse(result)

To access the child properties, you can do another for in loop inside.

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

Comments

1

JSON creates normal Javascript objects.

You can access their properties just like those of any other object:

var myValue = myObject.features.x.visual_type;

4 Comments

Not all javascript environments parse JSON strings automatically
@Sammaye: are there any that do so?
@Qantas94Heavy No, I don't think so, though using a library can make it seem so
thank you. I have a solution.I have updated my topic

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.