0

I have a str'ed dictionary, i.e '{}' instead of simply {}. How to exctract data from this dict and put into a table?

Here is the dict:

requesterArcticresReservations = 
'{'A20573': {'reservation_url': 'https://sst.arcticres.com/reservation/20573',
            'trip_name': 'Skagway Triple Adventure',
            'datetime': '8/7/16 at 12:45PM', 
            'visitors': '2 guys'
            }}'

Here is a sketch of the code I need:

(function() {
    "use strict";
    return {
        initialize: function() {
            var requesterArcticresId = domHelper.ticket.getContactInfo().user.custom_field.cf_arcticres_person_id;
            var requesterArcticresReservations = domHelper.ticket.getContactInfo().user.custom_field.cf_arcticres_reservations;
            var reservations_html;

            if (requesterArcticresId) {
                var d = JSON.parse(requesterArcticresReservations);

                // what to write here in order to add data to the table?

                reservations_html =+ 
                    '<table><tr>' +
                        '<td><a' + reservation_url + '>' + id + '</a></td>' +
                        '<td>' + trip_name + '<br>' + visitors + '<br>' + datetime '</td>' + 
                    '</tr></table>';
                jQuery(this.$container).find('#apptext').html(reservations_html);
                }
            else {
                jQuery(this.$container).find('#apptext').text("There is no data to show");
            }
            appPlaceholder.ticket.belowRequestorInfo(jQuery(this.$container));
        }
    };
})();

in Python I can exctract from dict all values and add them to the table, something like this:

reservations_html = ''
for id in dic:
    reservation_url = reservation_url['reservation_url']
    trip_name = id['trip_name']
    visitors = id['visitors']
    date_n_time = id['datetime']

    reservations_html =+ 
        '<table><tr>' +
            '<td><a' + reservation_url + '>' + id + '</a></td>' +
            '<td>' + trip_name + '<br>' + visitors + '<br>' + datetime '</td>' + 
        '</tr></table>';

How can I do the same in javascript?

FINAL WORKABLE VERSION

(function() {
    "use strict";
    return {
        initialize: function() {
            var requesterArcticresId = domHelper.ticket.getContactInfo().user.custom_field.cf_arcticres_person_id;
            var requesterArcticresReservations = domHelper.ticket.getContactInfo().user.custom_field.cf_arcticres_reservations;
            var reservations_html;

            if (requesterArcticresId) {
                var d = JSON.parse(requesterArcticresReservations);

                for (var id in d) {
                    if(d.hasOwnProperty(id)) {
                        var reservation_url = d[id]['reservation_url'];
                        var trip_name = d[id]['trip_name'];
                        var visitors = d[id]['visitors'];
                        var datetime = d[id]['datetime'];

                        reservations_html =+ 
                            '<table><tr>' +
                                '<td><a href=' + reservation_url + '>' + id + '</a></td>' +
                                '<td>' + trip_name + '<br>' + visitors + '<br>' + datetime + '</td>' + 
                            '</tr></table>';
                    }
                }

                jQuery(this.$container).find('#apptext').html(reservations_html);
                }
            else {
                jQuery(this.$container).find('#apptext').text("There is no data to show");
            }
            appPlaceholder.ticket.belowRequestorInfo(jQuery(this.$container));
        }
    };
})();

Thanks to @Felix Kling that showed me the right direction!

P.S. Found, why JSON.parser worked - I manually changes all keys\values from ' to " in admin panel.

TitanFighter
  • 4,582
  • 3
  • 45
  • 73
  • I don't see any `'{}'`? Looks like `d` is just an object (assuming `JSON.parse(requesterArcticresReservations)` works as expected)? – Felix Kling Dec 13 '16 at 18:57
  • Well, what you have is not valid JSON, so `JSON.parse(...)` is not going to work. Or do you have valid JSON and what you posted here doesn't reflect what you really have? – Felix Kling Dec 13 '16 at 19:00
  • fixed '{}' :). `JSON.parse(requesterArcticresReservations)` converts the dict to `Object`. – TitanFighter Dec 13 '16 at 19:00
  • @FelixKling, this dict is sent via API by Python and saved into a text field. – TitanFighter Dec 13 '16 at 19:01
  • It doesn't matter where it comes from. What you posted is not valid JSON, so `JSON.parse` wouldn't be able to parse it. But since you are saying that it does work, I assume you actually have valid JSON. In general, if the information you provide doesn't align with the state if your code / environment, it's more difficult for us to help you, since we don't know what is a real issue or just misinformation in the post. – Felix Kling Dec 13 '16 at 19:02
  • 1
    Also, since `JSON.parse` seems to be working, the question has nothing to do with "stringified dicts". You have an object and you seem to be asking how to access an object in JavaScript (which is basically the same as accessing a dict in Python). – Felix Kling Dec 13 '16 at 19:05
  • @Felix, thank you for the link `This question already has an answer here:` - I found the piece of info, that helped me. Dict in JS indeed works as in Python. My code already almost fixed. However, I tried to launch the code without `JSON.parse` and it does not work: `Uncaught TypeError: Cannot read property 'reservation_url' of undefined` in the line `var reservation_url = requesterArcticresReservations['A20573']['reservation_url'];`. Now Im trying to make `A20573` in a loop and the code is fixed. – TitanFighter Dec 13 '16 at 19:21

0 Answers0