Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Javascript JSON/ODATA get the path

0 Kudos

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

1 ACCEPTED SOLUTION

maheshpalavalli
Active Contributor
0 Kudos

Hi Bernd Dietrich

you need to do it manually.

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

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

4 REPLIES 4

maheshpalavalli
Active Contributor
0 Kudos

Hi Bernd Dietrich

you need to do it manually.

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

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

0 Kudos

Thanks for this information

0 Kudos

You can mark the answer as answered and close the question if your query is solved.

https://blogs.sap.com/2013/04/03/how-to-close-a-discussion-and-why/

BR,

Mahesh

0 Kudos

I have a dynamically struktur so I do this dynamically.

Does exist an other solution?