MatheusLPS MatheusLPS - 6 months ago 17
JSON Question

How to get the names of the inner JSON

I am taking some information from a database on a recordet, conventing it to JSON notation.

But now, I do not know how to read only the names of my columns.

See this example:

http://www.jsoneditoronline.org/?id=1bccb6d9522392f4763d58e24ff4a8e6

I need to read the GND_AREA, DCR_AREA names.

EDIT:

Some folks give some answers below that works, but I think that I did not explain correctly what I want.

My object has a key named "List", and since that JSON has this construction:

JSON

I want to retrieve the string names. Not the values....

Exemple: I want those strings below because those strings are my columns names, and they can change depending on with table I search for my data.

GND_AREA, DCR_AREA, DCR_EQUIP and etc.....

P.S: Really new to javascript here

END EDIT




I tried all possibilities I found here, but I do not know if my JSON notation is right....

I trying to do this with java script.

last one I tried was this one:

Object.keys(obj).forEach(function (key) {
console.log (obj[key].name)
});


Here my string:

{
"List": [
{
"GND_AREA": "Redução",
"DCR_AREA": "Alto Forno 1",
"DCR_EQUIP": "AF1-Conjunto de Sopro 01 a 28",
"DCR_TECNCA": "Monitoramento",
"DCR_DCPLNA": "Temperatura",
"COD_POSIC": "61019",
"DCR_POSIC": "OB-F1-RE-ANVE-CJSO-01A05",
"DCR_POSFUN": "Conjunto de Sopro 01-05 do Anel de Vento do Alto Forno 1",
"DCR_PNTINS": "Algaraviz 1",
"DAT_INSP": "23/2/2016 21:28:48",
"VLR_DP_DESVIO": "",
"VLR_LTURA": "450",
"VLR_DMA_DESVIO": "350",
"COD_UNDMED": "°C "
},
{
"GND_AREA": "Redução",
"DCR_AREA": "Alto Forno 1",
"DCR_EQUIP": "AF1-Conjunto de Sopro 01 a 28",
"DCR_TECNCA": "Monitoramento",
"DCR_DCPLNA": "Temperatura",
"COD_POSIC": "61019",
"DCR_POSIC": "OB-F1-RE-ANVE-CJSO-01A05",
"DCR_POSFUN": "Conjunto de Sopro 01-05 do Anel de Vento do Alto Forno 1",
"DCR_PNTINS": "Algaraviz 1",
"DAT_INSP": "24/2/2016 21:37:49",
"VLR_DP_DESVIO": "",
"VLR_LTURA": "140",
"VLR_DMA_DESVIO": "350",
"COD_UNDMED": "°C "
}
]
}

Answer

This might help explain. Put these functions in your code and call init() it will display the properties of your JSON in the console.

function display(obj, lvl) {
    var tab = Array(lvl + 1).join("\t");
    for (var property in obj) {
        var type = typeof obj[property];
        if (obj.hasOwnProperty(property)) {
            console.log(tab+"'"+property + "' of type "+type+" and value: "+obj[property]);
        }
        if (type == 'object') {
            console.log(tab+"which has the following properties:");
            display(obj[property], lvl + 1);
        }
    }
}
function init() {
    var json = '{"List":[{"GND_AREA": "Redução","DCR_AREA": "Alto Forno 1","DCR_EQUIP": "AF1-Conjunto de Sopro 01 a 28","DCR_TECNCA": "Monitoramento","DCR_DCPLNA": "Temperatura","COD_POSIC": "61019","DCR_POSIC": "OB-F1-RE-ANVE-CJSO-01A05","DCR_POSFUN": "Conjunto de Sopro 01-05 do Anel de Vento do Alto Forno 1","DCR_PNTINS": "Algaraviz 1","DAT_INSP": "23/2/2016 21:28:48","VLR_DP_DESVIO": "","VLR_LTURA": "450","VLR_DMA_DESVIO": "350","COD_UNDMED": "°C "},{"GND_AREA": "Redução","DCR_AREA": "Alto Forno 1","DCR_EQUIP": "AF1-Conjunto de Sopro 01 a 28","DCR_TECNCA": "Monitoramento","DCR_DCPLNA": "Temperatura","COD_POSIC": "61019","DCR_POSIC": "OB-F1-RE-ANVE-CJSO-01A05","DCR_POSFUN": "Conjunto de Sopro 01-05 do Anel de Vento do Alto Forno 1","DCR_PNTINS": "Algaraviz 1","DAT_INSP": "24/2/2016 21:37:49","VLR_DP_DESVIO": "","VLR_LTURA": "140","VLR_DMA_DESVIO": "350","COD_UNDMED": "°C "}]}';
    var obj = JSON.parse(json);
    console.log("My JSON has properties named:");
    display(obj, 1);
}

P.S. obj.property is syntactic sugar for obj['property']

Comments