-3
{
  "info": [
    {
      "subinfo": {
        "name": "ABC",
        "age": 23,
        "sex": "M",
        "addr": "xyz",
        "regDt": "01-Jan-2021"
      },
      "city": "NY",
      "eduInfo": {
        "deg": "BA",
        "master": "PhD"
      },
      "sports": {
        "indoor": "poker",
        "outdoor": "hockey"
      }
    },
    {
      "subinfo": {
        "name": "PQR",
        "age": 23,
        "sex": "F",
        "addr": "def",
        "regDt": "01-Jan-2021"
      },
      "city": "NY",
      "eduInfo": {
        "deg": "BA",
        "master": "NA"
      },
      "sports": {
        "indoor": "poker",
        "outdoor": "hockey"
      }
    }
  ]
}

The above data is a simple example of what kind of data I am working with. There are such hundreds of info's basically of Males and Females. I need two separate lists for both, Males and Females. So, to extract the data of Males i.e; sex="M", I am using this condition

data = json.loads(data)

for m in data['info'] :
    if m['subinfo']['sex'] == "M" :
            mList = m

print(mList)

#and for Females list

for f in data['info'] :
    if f['subinfo']['sex'] == "F" :
            fList = f

print(fList)

I am expecting more than 1 record for each, Males and Females, but the actual result is only one for each. What's wrong with this code?

Ouroborus
  • 16,237
  • 4
  • 39
  • 62
John
  • 2,820
  • 3
  • 30
  • 50
  • 1
    your json is plainly wrong. It misses commas, proper formatting and there is a double key `info` which results to a single entry on load. – Commissar Vasili Karlovic Mar 20 '22 at 09:33
  • 1
    setting aside that you cannot have duplicate key "info" in json, is the value of it a list (array) or dict (object)? Currently there's a `[` opened but never closed – re-za Mar 20 '22 at 09:52
  • @re-za Actually, duplicate keys are valid in JSON. See [Does JSON syntax allow duplicate keys in an object?](https://stackoverflow.com/questions/21832701/does-json-syntax-allow-duplicate-keys-in-an-object) – Ouroborus Mar 20 '22 at 10:15
  • @CommissarVasiliKarlovic There's a single `info`. Please check. – John Mar 20 '22 at 10:48
  • @CommissarVasiliKarlovic Before posting, I have checked the json format online. Here - https://jsonformatter.curiousconcept.com/ – John Mar 20 '22 at 10:56
  • @John Ouroborus edited the original json to comply with his answer (lol). The website you posted gives `6 Errors` in the original... – Commissar Vasili Karlovic Mar 20 '22 at 11:07
  • @CommissarVasiliKarlovic Yes. Ouroborus pointed this out. The original one which I edited, a few information, had errors and validated it. It seems I posted the wrong one. My fault. A serious crime. Apologies. But there's only one `info`. For sure. – John Mar 20 '22 at 11:17

1 Answers1

0

You set the variable to the object each time. Because of this, the variable will be whatever the last matching object was. Instead, set the variable to an empty list and then append the objects to that list.

mList = []
for m in data['info']:
    if m['subinfo']['sex'] == 'M':
         mList.append(m)
Ouroborus
  • 16,237
  • 4
  • 39
  • 62
  • Well, the empty lists were declared in code, which I didn't share. Yes. `append()` was the must. Thanks a lot. :) – John Mar 20 '22 at 09:29
  • 1
    the answer is wrong. Since the key `info` is a single entry, there aren't multiple records to append on. Thus, there are either multiple unique keys `info1`, `info2`, `info3` and you should remove the `info` from that bracket or there's no answer at all. – Commissar Vasili Karlovic Mar 20 '22 at 09:44
  • 1
    @CommissarVasiliKarlovic Asker seems to disagree with you. I suspect the JSON they provided is not representative of their actual data. – Ouroborus Mar 20 '22 at 09:55
  • @Ouroborus the json they provided is not valid anyways (e.g. duplicate keys). And how did you decide that "info" is a list (array) not a dict (object) ? it's not clear in the question. If you have the privilege to do so, consider editing the question. – re-za Mar 20 '22 at 09:57
  • 1
    @re-za The JSON is irrelevant to the issue. – Ouroborus Mar 20 '22 at 10:10
  • @Ouroborus I suspect the OP just stripped out the top level keys leaving only `info` and that's why your answer works for him. Since you have already given an accepted answer, I suggest to point out the `info` issue and state explicitly under which assumptions you answer. – Commissar Vasili Karlovic Mar 20 '22 at 10:19
  • @CommissarVasiliKarlovic I've opted to update the JSON in the question so that it is correct and fits the intent of the question. – Ouroborus Mar 20 '22 at 10:21
  • @CommissarVasiliKarlovic `info` is the top level key. I havent stripped the json file. – John Mar 20 '22 at 10:51
  • @Ouroborus If you want to update the JSON, you can, but I haven't stripped it. Its just AS IT IS. I have edited ONLY `name` and `addr` – John Mar 20 '22 at 10:54
  • 1
    @John If that were the case, you would have gotten `JSONDecodeError` since the JSON as originally provided in the question is indeed invalid. – Ouroborus Mar 20 '22 at 11:02
  • @Ouroborus Let me check I still have the original and the formatted one. Gimme 2 minutes. – John Mar 20 '22 at 11:08
  • @Ouroborus Yes. I posted the wrong one. My fault. I validated it online but seems, I have posted the wrong one. Apologies. – John Mar 20 '22 at 11:12