0

I would like to address an unknown field in my json structure, which is like below. I would like to address the gateways in that structure, I have reached till semt.gateways where I got those field but now I want to have access to AAA and BBBB, but those values (AAA or BBBB) might appear or not depending on the gateway status, so it might be as well CCCC or DDDD two new gateways.

Is there a way to addressing those fields?

Thanks in advance,

{
    "router": {
        "broadcast": {
            "count": 7,
            "rate_1": 0.010835197454271446,
            "rate_15": 0.02788453406371163,
            "rate_5": 0.005430725620930126
        },
        "send": {
            "count": 3,
            "rate_1": 0.0016871408896396796,
            "rate_15": 0.06962527561018554,
            "rate_5": 0.010521501218758188
        },
        "send_recipients": {
            "avg": 1,
            "max": 1,
            "min": 1,
            "p_25": 1,
            "p_50": 1,
            "p_75": 1
        },
        "stat": {
            "in": {
                "count": 145,
                "rate_1": 0.06743374503314854,
                "rate_15": 0.058519644434074265,
                "rate_5": 0.06641436790472173
            }
        },
        "uplink": {
            "in": {
                "count": 10,
                "rate_1": 0.012522320815414685,
                "rate_15": 0.02981672465254896,
                "rate_5": 0.008197385273343911
            },
            "negative_broker_response": {
                "count": 6,
                "rate_1": 0.010835197452761661,
                "rate_15": 0.027646729205473473,
                "rate_5": 0.005398046392581974
            },
            "out": {
                "count": 3,
                "rate_1": 0.0016871408896396796,
                "rate_15": 0.06962527561018554,
                "rate_5": 0.010521501218758188
            }
        },
        "waiting_for_send": {
            "count": 0
        }
    },
    "semt": {
        "gateways": {
            "AAA": {
                "last_pull_data": {
                    "date": "2016-08-03T12:06:21Z"
                },
                "last_push_data": {
                    "date": "2016-08-03T12:06:18Z"
                },
                "pull_data": {
                    "count": 295,
                    "rate_1": 0.1041642570745486,
                    "rate_15": 0.09416326315994832,
                    "rate_5": 0.10078387259336224
                },
                "push_data": {
                    "count": 102,
                    "rate_1": 0.05147652686982562,
                    "rate_15": 0.03543975773566928,
                    "rate_5": 0.03974814271702003
                }
            },
            "BBBB": {
                "last_pull_data": {
                    "date": "2016-08-03T12:06:22Z"
                },
                "last_push_data": {
                    "date": "2016-08-03T12:06:25Z"
                },
                "pull_data": {
                    "count": 145,
                    "rate_1": 0.10405362006983286,
                    "rate_15": 0.11860991513624394,
                    "rate_5": 0.10040848429373028
                },
                "push_data": {
                    "count": 53,
                    "rate_1": 0.028479538986283692,
                    "rate_15": 0.06878462929928854,
                    "rate_5": 0.03614229891544378
                }
            }
        },
        "pull_data": {
            "count": 440,
            "rate_1": 0.208217877139802,
            "rate_15": 0.17371766572905512,
            "rate_5": 0.19970304027290767
        },
        "push_data": {
            "count": 155,
            "rate_1": 0.07995606584855905,
            "rate_15": 0.06384508343602703,
            "rate_5": 0.07424449222265977
        }
    }
}
ndarkness
  • 1,011
  • 2
  • 16
  • 36
  • Possible duplicate of [Getting JavaScript object key list](http://stackoverflow.com/questions/3068534/getting-javascript-object-key-list) – str Aug 03 '16 at 13:11

1 Answers1

0

Why don't you just do console.log(Object.keys(obj.semt.gateways)). Below is a fiddle, let me know if you need something else.

https://jsfiddle.net/yk48b9xr/

Thalaivar
  • 23,282
  • 5
  • 60
  • 71
  • thanks but I would like to address only the fileds AAA or BBB, it's to say their names not the content – ndarkness Aug 03 '16 at 13:22
  • How about Object.keys(obj.semt.gateways).filter(key=>{ return key=='AAA' or key=='BBB'}) (es6 syntax). I generally agree with his answer. If this doesn't work for you I'd consider rewording and giving some more examples of what you mean exactly. – Bradford Medeiros Aug 03 '16 at 13:30
  • I have edited the post, I mean that AAA, BBB might turn up or not depinging on the gateway, but for instance a newer one could pop up, like CCCC, so I would like to be able to address CCCC or the name of that GW without knowing it in advance. – ndarkness Aug 03 '16 at 14:35
  • You would get the names right... console.log(Object.keys(obj.semt.gateways)) by doing this in an array of keys whose name is not known for you.. – Thalaivar Aug 03 '16 at 14:38