0

I'm pretty new to Python, so just working my way through understanding the data sets.

I'm having a little trouble producing the JSON output that is required for the API I am working with.

I am using

import json
json.load(data_file)

Working with Python dictionary and then doing

json.dump(dict, json_data)

My data needs to look like the following when it is output.

    {
  "event":{
    "id":10006,
    "event_name":"My Event Name",
  },
  "sub event":[

  ],
  "attendees":[
    {
      "id":11201,
      "first_name":"Jeff",
      "last_name":"Smith",
    },
    {
      "id":10002,
      "first_name":"Victoria",
      "last_name":"Baker",
    },
  ]
}

I have been able to create the arrays in python and dump to json, but I am having difficulty creating the event "object" in the dictionary. I am using the below:

attendees = ['attendees']
attendeesdict = {}
attendeesdict['first_name'] = "Jeff"
attendees.append(attendeesdict.copy())

Can anyone help me add the "event" object properly?

3
  • You're having difficulty with the Event object/dcit but your question seems to be about the Attendees dict. Please clarify. You probably will not be able to use .copy method here if done inside a loop, but you need to show more code so we can understand what you're actually attempting to implement, and how you're going abou it. Commented Oct 31, 2016 at 13:13
  • 1
    The code you have so far makes no sense anyway. There is no need to have the string 'attendees' as the first element in the attendees list. Commented Oct 31, 2016 at 13:13
  • It is difficult to understand what you are asking for. What is in you "data_file" ? please post its content. Your event seems to be a very simple dictionary so what is stopping you from doing a simple json_data["event"] = { "id":10006, "event_name":"My Event Name" } ? You will notice that Python dict/list and JSON are very much look-alike. Commented Oct 31, 2016 at 13:14

4 Answers 4

1

In general, going from JSON to dictionary is almost no work because the two are very similar, if not identical:

attendees = [
    {
        "first_name": "Jeff"
        # Add other fields which you need here
    },
    {
        "first_name": "Victoria"
    }    
]

In this instance, attendees is a list of dictionaries. For the event:

event = {
    "id": 10006,
    "event_name": "My Event Name"
}

Putting it all together:

data = {
    "event": event,
    "sub event": [],
    "attendees": attendees
}

Now, you can convert it to a JSON object, ready to send to your API:

json_object = json.dumps(data)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much! This explained it perfectly for me, I was making things so overcomplicated by iterating through all the lists, python makes it so simple when you see it like this!
1

Assuming you have built all the values elsewhere and now you're just putting them together:

result = {'event':event_dict, 'sub event':subevent_list, 'attendees':attendees_list}

Comments

1

If you want just to statically create a nested dict, you can use a single literal. If you paste the JSON above into python code, you would get a valid dict literal.

Comments

0

Construct your dicts and add like below

    {
      "event":"add your dict"
      "sub event":["add your dict"],
      "attendees":["add your dict"]
    }

Comments

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.