JSON Root Object JS with Square bracket in code


(Kieran@parracity) #1

Hi all!


So I am not a "guru" on JS, and I need help defining the "JSON Root Object" in a JSON Data Source.



My problem is that we need to define the root node in simple JS dot notation, which would normally be fine, however my JSON Response starts with the following:

    {
      "events": [u][[/u]
    {
      "event": {
        "box_header_text_color": "005580",
        "link_color": "EE6600",
        "box_background_color": "FFFFFF",
        "timezone": "Australia/Sydney",


See, the square bracket, [b][u][[/u][/b], opening seems to be ruining everything. I can only use "events" as the JSON Root Object, but not "events.event".
When using "events" it passes fine, but will not split the code up into nodes and therefor, I cannot use keywords.

How would I write this in the JSON Root Object, so that my Root is "event" and not "events"?
To get a sample/https URL, go to developer.eventbrite.com/doc/organizers/organizer_list_events/, click "TRY IT NOW" on the top right, and use the ID "1314301".

Thanks in advance to all you saavy developers!

(Dan Simmons) #2

Hi there,


The root object you are looking for is "events[0].event". I tested this on my system with the JSON you linked to and it worked fine.



I spotted a problem with the Eventbrite site you linked to though - when you click the "Try It Now" button and enter an ID, the "Example Response" JSON it prints out on the screen is not valid (it has real line breaks in the middle of strings, which is not allowed). However, when you do the actual API request the JSON response is valid. A small glitch but this might be the problem if you're getting "JSON Parse Error".



:slight_smile:


(Kieran@parracity) #3

[quote]
Hi there,



The root object you are looking for is "events[0].event". I tested this on my system with the JSON you linked to and it worked fine.



I spotted a problem with the Eventbrite site you linked to though - when you click the "Try It Now" button and enter an ID, the "Example Response" JSON it prints out on the screen is not valid (it has real line breaks in the middle of strings, which is not allowed). However, when you do the actual API request the JSON response is valid. A small glitch but this might be the problem if you're getting "JSON Parse Error".



:slight_smile:

[/quote]



Hi Dan,



Firstly, thanks so much for testing this and providing your fast response!



I now have two questions, if you're up to continue helping…

You've used Array "[0]" in the JSON Root Object, but does this mean if I have several events and I'm doing "organizer_list_events" (AKA list the events of an organizer with the supplied id), it will only use the first "event" in the "events" array?



Secondly, "Available Keywords" in matrix still dont show up anything other than "%data_source_record_set_events%", event when I use "link_color" or any other attribute as "JSON Data Attributes. I need to be able to use the attributes…



Please forgive my ignorance; as you can tell, this is not a part of Matrix I have used before and Eventbrite API doesn't seem to make life easy.



Kind thanks in advance,



Kieran


(Dan Simmons) #4

Not a problem, happy to help :slight_smile:


If you explicitly specify the first element of an array as the root object, then you won't be able to work with multiple objects in that array. So that's not what you want.



However, even if the root object you are working with has other JS objects or arrays several levels deep, you can still work with them, but you have to manually specify the attributes you need using the "JSON Data Attributes" field.



For example:



Suppose you want to print a list of events from Eventbrite. You want to print the event title, date, description, organiser and the price of tickets.



Firstly, the events are located in an array called "events", so that's what you should put into the "JSON Root Object" field. Next, use JS "notation" to enter a newline-separated list of the attributes you want relative to the root object you've specified. So in this case, that would be:



  • event.title
    [*]event.start_date
    [*]event.description
    [*]event.tickets[0].ticket.price
    [*]event.organizer.name


After entering that into the "JSON Data Attributes", you should get a list of available keywords that look like this:

    
    Keyword	Description
    %ds__event%	Record Set Attribute : event
    %ds__event.title%	Record Set Attribute : event.title
    %ds__event.start_date%	Record Set Attribute : event.start_date
    %ds__event.description%	Record Set Attribute : event.description
    %ds__event.tickets_0.ticket.price%	Record Set Attribute : event.tickets_0.ticket.price
    %ds__event.organizer.name%	Record Set Attribute : event.organizer.name


If you go to the details screen of one of the top data source record, you should see:

    
    	Record Set Is:
    event	{"box_header_text_color":"005580","link_color":"EE6600","box_background_color":"FFFFFF","timezone":"Australia\/Sydney","box_border_color":"D5D5D3","logo":"http:\/\/ebmedia.eventbrite.com\/s3-s3\/eventlogos\/17896083\/2973703427-1.png","organizer":{"url":"http:\/\/parracity.eventbrite.com","description":"","id":1323501107,"name":"Parramatta City Council"},"background_color":"FFFFFF","id":2147483647,"category":"testing,other","box_header_background_color":"EFEFEF","capacity":0,"num_attendee_rows":0,"title":"API Test Event","start_date":"2012-03-27 07:00:00","status":"Live","description":"API Test Event Details","end_date":"2012-03-27 23:00:00","tags":"API, test","text_color":"005580","title_text_color":"","tickets":[{"ticket":{"description":"API Test Description","end_date":"2012-03-29 12:00:00","min":1,"max":2,"price":"0.00","visible":"true","start_date":"2012-02-15 01:00:00","currency":"AUD","type":0,"id":13006683,"name":"API Test Ticket"}}],"created":"2012-02-15 19:46:35","url":"http:\/\/www.eventbrite.com\/event\/2973703427","box_text_color":"000000","privacy":"Public","venue":{"city":"Parramatta","name":"Parramatta City Council","country":"Australia","region":"New South Wales","longitude":151.010816,"postal_code":"2150","address_2":"","address":"30 Darcy St","latitude":-33.788676,"country_code":"AU","id":1162333,"Lat-Long":"-33.788676 \/ 151.010816"},"modified":"2012-02-15 20:16:55","logo_ssl":"https:\/\/ebmedia.eventbrite.com\/s3-s3\/eventlogos\/17896083\/2973703427-1.png","repeats":"no"}
    event.title	API Test Event
    event.start_date	2012-03-27 07:00:00
    event.description	API Test Event Details
    event.tickets_0.ticket.price	0.00
    event.organizer.name	Parramatta City Council


All the attributes we specified in the list should now be visible on the details screen and have data in them.

Note that in the example above I assumed there is only one ticket type (as I'm pulling just the first ticket price, using event.tickets[0].ticket.price). If you wanted to iterate over the ticket types as well, it's starting to get complicated so there are two things you could do:

[list=1]
[*]Use two JSON Data Source assets; one with a dynamic root node of session variable "list_current_asset_id" so that in a listing you could pull in the "event.tickets" JSON data and list it (a bit complicated)
[*]Or, more appropriately, use a REST Resource JS asset to point at the Eventbrite feed first. You can then use server-side JavaScript to manipulate the feed, ouputting only the information you need in a flat JSON array. The JSON Data Source asset is then designed to be able to point at this REST asset. This gives you powerful data processing ability with the flexbility of using an asset listing for the presentation.
[/list]

Hope this helps :)

(Kieran@parracity) #5

Oh my god!


I have no idea what I was doing wrong.

I swear I had tried this and what you said completely made sense, but now it works!



I'm just going to say "err Matrix just wasn't working properly"

>>

<
<



Thank you SOO MUCH for your help and long explanation Dan!