-1

Suppose I have a JSON object:

obj= [{"name":"Era", "age":45, "sex":"female", "id":2545}  
     {"name":"Patrick", "age":35, "sex":"male", "id":2546}  
     {"name":"Elina", "age":40, "sex":"female", "id":2547}  
     {"name":"Reg", "age":47, "sex":"male", "id":2548}]   

I want to make a (key,value) RDD from this data by only using the 'id' and the 'name' ('id' being the key in RDD). I tried the solution given in this link but getting the following error:

AttributeError: 'str' object has no attribute 'get'

To explain more here is my code-

for key in obj:
    my_dict={}
    my_dict['id']=key.get('id')
    my_dict['name']=key.get('name')
    result.append(my_dict)  

I hope to get any help for this part so that I can go for the second part i.e. to make an rdd from it.

3 Answers3

1

You can write it in one line, this must work if your obj is correct

result = [{'id': item.get('id'), 'name': item.get('name')} for item in obj]

I tested your code in my own env, only problem what I get, is that in obj you need to add commas between dicts

1

Works for me when I fix the JSON in obj:

In [4]: obj= [{"name":"Era", "age":45, "sex":"female", "id":2545},
   ...:      {"name":"Patrick", "age":35, "sex":"male", "id":2546},
   ...:      {"name":"Elina", "age":40, "sex":"female", "id":2547},
   ...:      {"name":"Reg", "age":47, "sex":"male", "id":2548}]

In [6]: result = []

In [7]: for key in obj:
   ...:     my_dict={}
   ...:     my_dict['id']=key.get('id')
   ...:     my_dict['name']=key.get('name')
   ...:     result.append(my_dict)
   ...:

In [8]: result
Out[8]:
[{'id': 2545, 'name': 'Era'},
 {'id': 2546, 'name': 'Patrick'},
 {'id': 2547, 'name': 'Elina'},
 {'id': 2548, 'name': 'Reg'}]
amanb
  • 5,276
  • 3
  • 19
  • 38
0

Try this if you want a simple dictionary with key values :

obj= [{"name":"Era", "age":45, "sex":"female", "id":2545},  
     {"name":"Patrick", "age":35, "sex":"male", "id":2546},  
     {"name":"Elina", "age":40, "sex":"female", "id":2547},  
     {"name":"Reg", "age":47, "sex":"male", "id":2548}] 

my_dict = {}
for i in obj:
    my_dict[i.get('id')] = i.get('name')

print(my_dict)

# Output: {2545: 'Era', 2546: 'Patrick', 2547: 'Elina', 2548: 'Reg'}
Schiz
  • 46
  • 3