Skip to Content

Javascript JSON/ODATA get the path

Hi Folks,

when i get JSON data like this.

// JSON sample data
var data = {
    company:{
        name:"Treefish Inc",
        info:{
            employees:3,},
        contacts:[{
                name:"Barbara",
                phone:"873"},{
                name:"Gerry",
                phone:"734"},{
                name:"Susan",
                phone:"275"}]}};

Absolute binding paths within this model when i know the structure of the data.

/company/name
/company/info/employees
/company/contacts

How can i get the sub paths when i have only the path /company.

Is there an function to get this?

Thanks for any help

Bernd

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

3 Answers

  • Best Answer
    Feb 22 at 06:52 AM

    Hi Bernd Dietrich

    you need to do it manually.

    https://stackoverflow.com/questions/7306669/how-to-get-all-properties-values-of-a-javascript-object-without-knowing-the-key

    var data = {
        company:{
            name:"Treefish Inc",
            info:{
                employees:3,},
            contacts:[{
                    name:"Barbara",
                    phone:"873"},{
                    name:"Gerry",
                    phone:"734"},{
                    name:"Susan",
                    phone:"275"}]}};
    // This will give you the properties inside the object
    var keys = Object.keys(data );
    // ["company"]
    var keys = Object.keys(data.company );
    // ["name", "info", "contacts"]
    
    // So you have to call it recursivvely to fetch all the paths...
    // You can also use
    for(var sProperty in data){
    //data[sProperty]
    }
    

    So you need to wirte a function to do it recursively and give you the paths.. Is there any reason to do that dynamically?

    BR,

    Mahesh

    Add comment
    10|10000 characters needed characters exceeded

  • Feb 22 at 11:01 PM

    Thanks for this information

    Add comment
    10|10000 characters needed characters exceeded

  • Feb 26 at 07:25 PM

    I have a dynamically struktur so I do this dynamically.

    Does exist an other solution?


    Add comment
    10|10000 characters needed characters exceeded