1

I'm trying to print in browser console with JS my JSON response from PHP. I saw many posts here and at another sites, but I can't do it.

I have a JSON like that

{"0":{"codigo_intervencao":"Apoio Emocional - 5270"},"1":{"tempo":"30"},"2":{"codigo_intervencao":"Apoio Emocional - 5270"},"3":{"tempo":"30"},"4":{"codigo_intervencao":"Apoio Emocional - 5270"},"5":{"tempo":"231518"},"6":{"codigo_intervencao":"Apoio Emocional - 5270"}}

I want to print in console, for example, each value of the key "codigo_intervencao", but I cannot achieve that.

I've tried something like that:

$(document).ready(function() {

  $( "#target" ).click(function() {
    console.log("Select JS");
      $.ajax({
        type: "POST",
        url: "select.php",
        dataType: 'html',
        success: function(response) {
            console.log("Response:" + response); // Here it prints above json
            var array = $.parseJSON(response);
            var tempo = 0;
            var arrayLength = array.length;

            for (var i = 0; i < arrayLength; i++) {
              console.log(array[i]['codigo_intervencao']);

        }
      });
  });
}); 
Alan Godoi
  • 657
  • 1
  • 12
  • 39
  • I guess your JSON is an object, not an array. Try what `console.log(arrayLength)` gives you. – Thomas Landauer Feb 28 '18 at 23:38
  • It gives me "undefined". So is it an object? How can I handle it? – Alan Godoi Feb 28 '18 at 23:39
  • You don’t have a `length` property. Iterate over [`Object.keys`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys), [`Object.entries`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries) or [`Object.values`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values) instead. – Sebastian Simon Feb 28 '18 at 23:42
  • Several options: 1. If it is an array in PHP, use PHP's `json_encode()` to get an array in JavaScript. 2. Check if you need `$.parseJSON()` at all. 3. Try `for...in`: https://www.w3schools.com/jsref/jsref_forin.asp – Thomas Landauer Feb 28 '18 at 23:43
  • try `console.table(array)` – mpen Feb 28 '18 at 23:45
  • Here's an example I put together before this thread was closed: https://jsbin.com/netexuh/edit?js,console – Jason Feb 28 '18 at 23:48

2 Answers2

2

You have a JSON object not an array.

var elements = $.parseJSON(response)
for (element in elements){
    console.log(elements[element]['codigo_intervencao'])
}

Iterate the JSON and get element you want.

Wesgur
  • 3,140
  • 3
  • 18
  • 28
1

The JSON example you provided would parse to an Object. You could try something like:

const data = JSON.parse(response);
const array = Object.keys(data).map(key => data[key]);
array.forEach(item => console.log(item.codigo_intervencao))

You'll get undefined for the objects that don't have the codigo_intervencao key. Alternatively you could iterate through your object with for..in.

jens
  • 2,075
  • 10
  • 15