0

I am new to python. I want to extract the elements of array 'Address' from below json. I am trying to use map to split the elements using

r=<below json>
s=r["Customer"]
y=s.map(lambda x:x.split(",")) 

But I am getting the error as .....AttributeError: 'str' object has no attribute 'map'

Can you please advise which is the best way to do this.

{ "id": "981",
  "Customer": 
[
  {
    "Name": "abc",
    "Title": "Mr",
    "Gender": "M",
    "Address": [
      {
    "Postcode": "2GF",
    "StreetName": "Black Street",
    "FlatNo": "123",
      }
    ]
  },
  {
    "Name": "xyz",
    "Title": "Mrs",
    "Gender": "F",
    "Address": [
      {
    "Postcode": "2CX",
    "StreetName": "White Street",
    "FlatNo": "123",
      }
    ]
  }
]
}
jakrm
  • 183
  • 2
  • 3
  • 11
  • This doesn't answer your question, but consider using the built-in `json` module to parse json instead of splitting values out of it manually. – Kevin Mar 15 '19 at 13:19
  • 2
    What is `r` when you are using `r.map()`? – Brendan Martin Mar 15 '19 at 13:19
  • First and foremost: Welcome to Stack Overflow! Now: As Kevin already pointed out, you should probably parse the string with the `json` module first. – CodenameLambda Mar 15 '19 at 13:19
  • http://book.pythontips.com/en/latest/map_filter.html#map – naivepredictor Mar 15 '19 at 13:21
  • 1
    I'm not aware of a single python data structure that has a `.map` method. Where'd you get that from? – Aran-Fey Mar 15 '19 at 13:21
  • `map` in Python is a "plain function", not a method on any object type. (This is inconvenient in my opinion, but thankfully Python has things like list comprehensions which are a very nice syntax for most mapping/filtering operations.) Further, it applies to lists rather than strings - you could convert the string to a list first with `.split`. – Robin Zigmond Mar 15 '19 at 13:26
  • 1
    I'm confused. Is `r` a string or a dict? If it's a string, then `s=r["Customer"]` should crash with `TypeError: string indices must be integers`. If it's a dict and if the value associated with "Customer" is a list, then `s.map` should crash with `AttributeError: 'list' object has no attribute 'map'`. In either case, you should not be getting `AttributeError: 'str' object has no attribute 'map'`. Can you provide a [mcve]? – Kevin Mar 15 '19 at 13:29
  • "I'm not aware of a single python data structure that has a .map method. Where'd you get that from? – Aran-Fey" check the pyfunctional and chained operators with seq – naivepredictor Mar 15 '19 at 13:40
  • @Aran-Frey I am used to pyspark a little bit. so I tried to use map and split from pyspark. – jakrm Mar 15 '19 at 13:48
  • Kevin, any references to built-in json module? – jakrm Mar 15 '19 at 13:52

2 Answers2

0

"I want to extract the elements of array 'Address' from below json":

[x for dd in r['Customer'] for x in dd['Address']]
naivepredictor
  • 898
  • 4
  • 14
0

Why not just:

data = { id: 981,
  'Customer':
[
  {
    "Name": "abc",
    "Title": "Mr",
    "Gender": "M",
    "Address": [
      {
    "Postcode": "2GF",
    "StreetName": "Black Street",
    "FlatNo": "123",
      }
    ]
  },
  {
    "Name": "xyz",
    "Title": "Mrs",
    "Gender": "F",
    "Address": [
      {
    "Postcode": "2CX",
    "StreetName": "White Street",
    "FlatNo": "123",
      }
    ]
  }
]
}


for item in data['Customer']:
    for data_item in item['Address']:
        print(data_item['Postcode'], data_item['StreetName'], data_item['FlatNo'])

OUTPUT:

2GF Black Street 123
2CX White Street 123
DirtyBit
  • 16,613
  • 4
  • 34
  • 55