0

When data come from JSON in the format below, I can turn it into a dictionary in Python. However, the information is not always in the same order. For instance, first_name, I would assume is always 0. However, when I get the data, depending on the form used, may be in the 2 position. So now, instead of storing first_name in the right db field, it may be in the email field.

Here's my question: how do I ensure the value matches the db field using the name key, value?

{
  "name": "first_name", 
  "values": [
    "Joe"
  ]
},
{
  "name":"last_name",
  "values": [
    "Example"
  ]
},
{
  "name": "email", 
  "values": [
    "joe@example.com"
  ]
}

Thank you, as always! :-)


Update Here's how I access the code:

first_name = data['field_data'][0]['values'][0].encode('utf-8')
last_name = data['field_data'][1]['values'][0].encode('utf-8')
email = data['field_data'][2]['values'][0].encode('utf-8')
telephone = data['field_data'][3]['values'][0].encode('utf-8')

I'm making the assumption that first_name will always be at 0, even though that's not the case. The Python saves the data, it's putting emails in places where first_name should be. I'm trying to find a way to verify the JSON name key corresponds with the same db field before submitting the data.

joshlsullivan
  • 1,375
  • 2
  • 14
  • 21
  • 1
    By accessing the dictionary appropriately? – Ignacio Vazquez-Abrams Jul 11 '16 at 01:08
  • 1
    Are you missing square-brackets? What you have is not valid JSON. – juanpa.arrivillaga Jul 11 '16 at 01:11
  • @juanpa.arrivillaga It's just an example. – joshlsullivan Jul 11 '16 at 01:18
  • @IgnacioVazquez-Abrams I need to make sure that when the form is submitted, the `name` key matches my db model and then submits the appropriate `values`. – joshlsullivan Jul 11 '16 at 01:19
  • 1
    @joshlsullivan Well, I'm not sure what you are talking about. Can you perhaps expand on you example or at least show us the code that isn't working, the results you get, and the results you expect? – juanpa.arrivillaga Jul 11 '16 at 01:19
  • Why are you submitting the name separately from the values in the first place? – Ignacio Vazquez-Abrams Jul 11 '16 at 01:20
  • Yeah, example JSON that's not a valid JSON without any code isn't really helpful. Also if you are generating the JSON yourself, you might want to do something like `{"data":[{"email":{"value":"joe@example.com"}}]}` – iScrE4m Jul 11 '16 at 01:23
  • I updated my question. I'm looking for a way to confirm that key `name` corresponds to the key `values` before submitting the data, instead of using its position in the dictionary. – joshlsullivan Jul 11 '16 at 01:25
  • @joshlsullivan You are *not* using its position in the dictionary. Dictionaries are unordered. This is why your question is confusing. You are probably getting a list of dictionaries. Is the problem that you can't know ahead of time the order in the list? Because that isn't an issue with the way the json is deserialized. – juanpa.arrivillaga Jul 11 '16 at 01:26
  • Related: https://stackoverflow.com/questions/8481380/is-there-a-json-equivalent-of-xquery-xpath – Naftuli Kay Jul 11 '16 at 01:35

2 Answers2

2

Instead of relying on the order which, as you indicated, is fragile, you should actually look for the element which has the name you want by iterating on the array.

For example:

data = {
    'field_data' : [
        {
          "name": "first_name", 
          "values": [
            "Joe"
          ]
        },
        {
          "name":"last_name",
          "values": [
            "Example"
          ]
        },
        {
          "name": "email", 
          "values": [
            "joe@example.com"
          ]
        }
    ]
}

def get_values(name):
    for data_element in data.get('field_data'):
        if data_element.get('name') == name:
            return data_element.get('values')

    return None

if __name__ == "__main__":

    first_name = get_values('first_name')[0]
    last_name = get_values('last_name')[0] 
    email = get_values('email')[0]

    print 'first_name: ' + first_name
    print 'last_name: ' + last_name
    print 'email: ' + email

In this example, the get_values function looks for the element which has the desired name. Then, at the very bottom of the example, we use it to retrieve first_name, last_name and email.

Philippe Aubertin
  • 1,031
  • 1
  • 9
  • 22
  • I love that you made it a function. I'm working inside a Django class and not sure how to make it there. I'll keep working on it, but this is exactly what I was looking for. Thank you! – joshlsullivan Jul 11 '16 at 14:18
0

assuming

data = {"field_data":[{
  "name": "first_name", 
  "values": [
    "Joe"
  ]
},
{
  "name":"last_name",
  "values": [
    "Example"
  ]
},
{
  "name": "email", 
  "values": [
    "joe@example.com"
  ]
}]}

This is fairly easy way to iterate over all of your fields and if you find a match for what you are looking for, just assign to it. So add more ifs for the rest of your desired data

for thing in data['field_data']:
    if thing['name'] == 'first_name':
        first_name = thing['values'][0]
iScrE4m
  • 882
  • 1
  • 12
  • 31