0

How can I access my json hash by key?

I have an ajax call

function populate_technician_info(public_info_id)
{
  var advantage_id = $('#admin_info').data('advantage_id');

  $.ajax({
    type: 'POST',
    url: '/ajax/get_technician_info',
    data: {'advantage_id': advantage_id},
    dataType: "json",
    success: function(tech_1_info, tech_2_info)
    {
      alert(tech_1_info);
      add_technician_row(tech_1_info, tech_2_info, public_info_id);
    }
  });
}

enter image description here and a java script function to populate the information return (for now it's just an alert).

function add_technician_row(tech_1_info, tech_2_info, public_info_id)
{
  public_info_id = '#' + public_info_id;

  $.each([tech_1_info, tech_2_info], function(index, value)
  {
    $.each(this, function (tech_index, tech_info)
    {
      alert("Tech 1: " + tech_info.empl_first_nm);  
    });
  });
}

My question is how can I access the values by key? I have tried the obvious tech_info.key But I'm having no luck, it returns undefined or a hash object. enter image description here

But If i just print tech_info I get enter image description here

So I know I have the correct object.

The data is definitely there I have double checked it by printing the values from perl to the terminal. enter image description here

my $technician_1_results = $self->employee_data_hub->resultset('Advantage')->search({employee_id => $tech_1_employee_id});
my $tech_1_info = $technician_1_results->first;
$self->render(json => {tech_1_info => $tech_1_info, tech_2_info => $tech_2_info});
gregnnylf94
  • 370
  • 3
  • 16
  • 1
    show part of you perl code where you return json to javascript. Did you read https://metacpan.org/pod/Mojo::JSON? and this is ajax example: https://gist.github.com/dvinciguerra/3452989 – ilux Oct 05 '17 at 22:54
  • Wow, no I did not... Thank you much, I'll dig in tomorrow morning. – gregnnylf94 Oct 05 '17 at 22:59
  • 3
    Please [edit] the question and include your Perl code. From the `alert` screenshot it looks like you are doing something wrong when converting the data to JSON. Did you possibly put the variable that holds the data structure into double quotes? Something like this (pseudo Perl code): `to_json "Tech 1" => "$result";` That would stringify the `$result` variable, and if that doesn't have an overload (unlikely), it would just show the address of the data structure and the class it's blessed in. Exactly what we see here. – simbabque Oct 06 '17 at 08:48
  • "*So I know I have the correct object.*" No, what you have is a string, not an object. Show your Perl code. – melpomene Oct 06 '17 at 12:06
  • @ilux I'm not sure that's what I want or need. I have been passing arrays using the same method I am displaying with no issues at all. For lack of time I may just stash all the values from the hash in an array while it's still in perl and pass that... – gregnnylf94 Oct 06 '17 at 14:26
  • @simbabque I added the piece of code to the question. No I do not have my variables in double quotes. But it just clicked that it's actually a hash reference not a hash. So now I wonder if that has anything to do with it. – gregnnylf94 Oct 06 '17 at 14:28
  • What's inside of `$tech_1_info` and `$tech_2_info`? That code looks fine, but it might have been mangled before. Show us all the code from where that variable comes alive. Also to debug, you might want to do a `use Data::Dumper; warn Dumper $tech_1_info;` above the line with `$self->render` and then look in the application log (should be your morbo output in the terminal). Include that in the question too. – simbabque Oct 06 '17 at 15:03
  • So now that we know it's about DBIx::Class, it's pretty obvious. I've found you a question/answer that works and will close as duplicate. Next time, please try to include the relevant facts. It didn't matter here that that it was related to Javascript, but it does matter that it's DBIx::Class. – simbabque Oct 06 '17 at 15:12
  • I didn't realize that would be such an important piece I guess. Sorry about that. I'll try to keep it more relevant next time. Thank you. – gregnnylf94 Oct 06 '17 at 15:17

0 Answers0