4

in my python server-side script i have the following

....


data = {}
data['id'] = id
data['organizer'] = organizer
data['eventStart'] = eventStartLocal
data['eventEnd'] = eventEndLocal
data['subject'] = subject
data['attendees'] = attendees
# Serializing Data 
json_data = json.dumps(data)
id += 1
print(json_data) #this is what the script returns 

In my front-end using jquery/javascript i have this..

$.ajax({
       type: 'REQUEST',
       dataType: 'text',
          url: 'json.py',
          success: function(data){
               console.log(data);
               obj = JSON.parse(data);
               },

            }).done(function(){

                console.log(obj)
            })

        });

my output is:

{"id": 0, "organizer": "Some Name", "eventStart": "09:30 AM", "eventEnd": "10:00 AM", "subject": "rental procedure", "attendees": "Some Name<br />Person 2<br />Person 3"}
{"id": 1, "organizer": "Some Name", "eventStart": "09:30 AM", "eventEnd": "10:00 AM", "subject": "rental procedure", "attendees": "Some Name<br />Person 2<br />Person 3"}
{"id": 2, "organizer": "Some Name", "eventStart": "09:30 AM", "eventEnd": "10:00 AM", "subject": "rental procedure", "attendees": "Some Name<br />Person 2<br />Person 3"}

My issue

When i try to parse the json in javascript, i get an error code about

Unexpected token { in JSON at position

After doing some research, i found that i would have to send a json array as a response and not print a jsoned line for each event, otherwise my javascript wont be able to parse it and put it in an object.

My Goal

To get data from server and pass it to javascript as an object so that i can loop through that object and create an HTML element for each event.

I'm new to this JSON stuff, somehow i need to do the following, but i don't seem to know how to create a json array (that has the [ ] and the , at the end of each json/line.

[
    {"id": 0, "organizer": "Some Name", "eventStart": "09:30 AM", "eventEnd": "10:00 AM", "subject": "rental procedure", "attendees": "Some Name<br />Person 2<br />Person 3"},
    {"id": 1, "organizer": "Some Name", "eventStart": "09:30 AM", "eventEnd": "10:00 AM", "subject": "rental procedure", "attendees": "Some Name<br />Person 2<br />Person 3"},
    {"id": 2, "organizer": "Some Name", "eventStart": "09:30 AM", "eventEnd": "10:00 AM", "subject": "rental procedure", "attendees": "Some Name<br />Person 2<br />Person 3"}
]

2 Answers 2

15

... the JSON array at the end of your answer is incorrect, but to generate an array, just give a list to json.dumps in Python. Something like json_data_list = []; ... ; json_data_list.append(json_data); ... print(json.dumps(json_data_list)); ...

Sign up to request clarification or add additional context in comments.

3 Comments

ahh i see that's the part that i was missing.. So the solution was to create a list and append my data to the list data_list.append(data) and then jsonify data_list. =) thank you!
@user234461 You saved my day ==> print(json.dumps(json_data_list)); that spells out to get the final appended list as json data. Thank you so much
@jAntoni That's great to hear, thank you for your comment :)
2

Your JSON file is incorrect. Normally you must have a structure as:

{
    "key1": [
        {
            "id": "blabla",
            "name": "Toto"
        },
        {
            "id": "blibli",
            "name": "Tata"
        }
    ],
    "key2": {
        "id": "value"
    },
    "key3": "value"
}

So I think you have to change your JSON array for example as following:

{
    [
        {
            "id": 0,
            "organizer": "Some Name",
            "eventStart": "09:30 AM",
            "eventEnd": "10:00 AM",
            "subject": "rental procedure",
            "attendees": "Some Name<br />Person 2<br />Person 3"
        },
        {
            "id": 1,
            "organizer": "Some Name",
            "eventStart": "09:30 AM",
            "eventEnd": "10:00 AM",
            "subject": "rental procedure",
            "attendees": "Some Name<br />Person 2<br />Person 3"
        },
        {
            "id": 2,
            "organizer": "Some Name",
            "eventStart": "09:30 AM",
            "eventEnd": "10:00 AM",
            "subject": "rental procedure",
            "attendees": "Some Name<br />Person 2<br />Person 3"
        }
    ]
}

You can decide also to have not a list of dictionary as I proposed above but to use the ID value as key for each dictionary; in that case you have:

{
    "id0":{       
            "organizer": "Some Name",
            "eventStart": "09:30 AM",
            "eventEnd": "10:00 AM",
            "subject": "rental procedure",
            "attendees": "Some Name<br />Person 2<br />Person 3"
    },
    "id1":{       
            "organizer": "Some Name",
            "eventStart": "09:30 AM",
            "eventEnd": "10:00 AM",
            "subject": "rental procedure",
            "attendees": "Some Name<br />Person 2<br />Person 3"
    },
    "id2":{       
            "organizer": "Some Name",
            "eventStart": "09:30 AM",
            "eventEnd": "10:00 AM",
            "subject": "rental procedure",
            "attendees": "Some Name<br />Person 2<br />Person 3"
    }
}

1 Comment

Your second JSON block is an object containing a list, but the list does not have a name. I believe bare lists are allowed in JSON (i.e. with no enclosing object), but if not, you need to give the list a name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.