1

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 "
    }
  ]
}
MatheusLPS
  • 65
  • 1
  • 7
  • Once the JSON has been converted into Javascript objects, the "List" property is just an array of objects. You can access these objects like you would any other object. To get the properties use `for (var property in obj)` see this: [How do I enumerate properties](http://stackoverflow.com/questions/85992/how-do-i-enumerate-the-properties-of-a-javascript-object). Don't forget to use `hasOwnProperty()` to only get members specified by your JSON – Tibrogargan May 18 '16 at 23:32

4 Answers4

2

Try this: Working Example

var t = setToYourString;

t.List.forEach(function(el){
  console.log(el.GND_AREA, el.DCR_AREA);
});

You want the properties on the object not the value:

t.List.forEach(function(el){
  var obj = el;
  for (var prop in el) {
    console.log(prop);
  }
});

https://jsbin.com/qudeda/6/edit?js,console

In your code, name is not on that object, the way you are try to read properties on the object require a key called name, which it doesnt. It does have List however.

omarjmh
  • 13,632
  • 6
  • 34
  • 42
2

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']

Tibrogargan
  • 4,508
  • 3
  • 19
  • 38
0

Let's say you store your string in a variable jsonString. Then, you could parse it and read the needed properties (here I store the information in two arrays).

var jsonString = JSON.parse('{"List": [{"GND_AREA": "Redução","DCR_AREA": "Alto Forno 1"},{"GND_AREA": "Aumento","DCR_AREA": "Alto Forno 2"}]}');

var gndAreas = [];
var dcrAreas = [];

for(var i = 0; i < jsonString.List.length; i++){

   gndAreas.push(jsonString.List[i].GND_AREA);
   dcrAreas .push(jsonString.List[i].DCR_AREA);

}
0

Thanks @JordanHendrix and @Tibrogargan!

Both methods worked! I just modified a little.

Method provided by @JordanHendrix:

var obj = t.List[0];
for (var prop in obj) {
  console.log(prop);
}

https://jsbin.com/gufubevozo/edit?js,console

Method provided by @Tibrogargan:

function display(obj2, lvl) {
var tab = Array(lvl + 1).join("\t");

var obj = obj2.List[0];

var i = 0;
var loop = 0;

for (var property in obj) {
    var type = typeof obj[property];

    if (obj.hasOwnProperty(property)) {
        console.log(property);                        
    } 
}

}

https://jsbin.com/qomunayiko/edit?js,console

I marked Tibrogargan´s answer because it is more complete.

MatheusLPS
  • 65
  • 1
  • 7