0

I got this little json file:

    {
    "description": {
        "is_a": "AnnotationProperty",
        "labelEN": "description",
        "labelPT": "descrição"
    },
    "relevance": {
        "is_a": "AnnotationProperty",
        "domain": "Indicator",
        "labelEN": "relevance",
        "labelPT": "relevância"
    },
    "title": {
        "is_a": "AnnotationProperty",
        "labelPT": "título",
        "labelEN": "title",
        "range": "Literal"
    }
}

I need to build a tree looking for the "is_a" field and the name before this field. Once I got these two fields, I can insert the child one on the tree in the right place.

So, using javascript, how can I get the name and the field "is_a" of each one?

I would like to have a loop statement that gives me all the names and "is_a" fields, for example, first time it gives me "description" and "AnnotationProperty" and the second iteration it gives me "relevance" and "AnnotationProperty", etc.

Thanks.

6
  • It is not json, it's yaml file! Commented May 1, 2016 at 20:04
  • I'm so sorry. I put the wrong one. Editing... Commented May 1, 2016 at 20:05
  • 1
    Please give a sample output you are expecting. Commented May 1, 2016 at 20:10
  • Sorry again. Edited. Commented May 1, 2016 at 20:16
  • Did it, trincot. Thanks. Commented May 1, 2016 at 20:22

2 Answers 2

1

You can list the names with their is_a property values like this:

Object.keys(data).forEach(function (name) {
   if (data[name].is_a) console.log(name + ' is a ' + data[name].is_a);
});

In a snippet:

var data = {
    "description": {
        "is_a": "AnnotationProperty",
        "labelEN": "description",
        "labelPT": "descrição"
    },
    "relevance": {
        "is_a": "AnnotationProperty",
        "domain": "Indicator",
        "labelEN": "relevance",
        "labelPT": "relevância"
    },
    "title": {
        "is_a": "AnnotationProperty",
        "labelPT": "título",
        "labelEN": "title",
        "range": "Literal"
    }
};

// collect name & is_a
result = [];
Object.keys(data).forEach(function (name) {
   if (data[name].is_a) result.push(name + ' is a ' + data[name].is_a);
});

// output in snippet
document.write(result.join('<br>'));

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

4 Comments

Thank yu so much!!! :D And just to know, is it possible to have a tree in js like it is in java or C, etc...? I'm asking this because it's my first time using js or json...
There are no native tree classes in Javascript, but you can implement it by nesting arrays and objects. There is a lot on the subject.
Done :D let me ask you...do you know how to convert the yaml file into json in a js function? And save it in a js variable like you did in our example. Thanks :D
It should be possible, but maybe it is better to ask a new question about that.
1

If your JSON string was valid, you could have parsed it into javascript object using JSON.parse() function.

And, if you want to iterate through an object, use built-in for in loop:

var json = {a:1, b:2, c:3};

for (var key in json) {
  // key
  // json[key] = {is_a: 'xxx', ...}
  // json[key][is_a]
}

Comments

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.