skip to Main Content

How to parse JSON response that includes multiple objects
with reference to above link, I am getting multiple JSON objects in response but unlike response in above link (which receives single array of multiple JSONs), I am getting multiple JSONs without the square brackets and comma between two JSONs

The response data is structured as follows:

{
"self": "https://example1.com",
"key": "keyOne",
"name": "nameOne",
"emailAddress": "mailOne",
"avatarUrls": {
  "48x48": "https://test.com/secure/useravatar?avatarId=1",
  "24x24": "https://test.com/secure/useravatar?size=small&avatarId=1",
  "16x16": "https://test.com/secure/useravatar?size=xsmall&avatarId=1",
  "32x32": "https://test.com/secure/useravatar?size=medium&avatarId=1"
},
"displayName": "displayNameOne",
"active": true,
"timeZone": "Europe",
"locale": "en_UK"
}
{
"self": "https://example2.com",
"key": "keyTwo",
"name": "nameTwo",
"emailAddress": "mailTwo",
"avatarUrls": {
  "48x48": "https://test.com/secure/useravatar?avatarId=2",
  "24x24": "https://test.com/secure/useravatar?size=small&avatarId=2",
  "16x16": "https://test.com/secure/useravatar?size=xsmall&avatarId=2",
  "32x32": "https://test.com/secure/useravatar?size=medium&avatarId=2"
},
"displayName": "displayNameTwo",
"active": false,
"timeZone": "Europe",
"locale": "en_US"
}

I tried looping through rest API response, thought of enclosing response within square brackets but all failed.

Parsing response with multiple json objects
I checked above link but here too but they have one JSON element in single line which is not the case with me. How to address the task using Python please help.

3

Answers


  1. Here is a Function:

    response = '''
    {
    "self": "https://example1.com",
    "key": "keyOne",
    "name": "nameOne",
    "emailAddress": "mailOne",
    "avatarUrls": {
      "48x48": "https://test.com/secure/useravatar?avatarId=1",
      "24x24": "https://test.com/secure/useravatar?size=small&avatarId=1",
      "16x16": "https://test.com/secure/useravatar?size=xsmall&avatarId=1",
      "32x32": "https://test.com/secure/useravatar?size=medium&avatarId=1"
    },
    "displayName": "displayNameOne",
    "active": true,
    "timeZone": "Europe",
    "locale": "en_UK"
    }
    {
    "self": "https://example2.com",
    "key": "keyTwo",
    "name": "nameTwo",
    "emailAddress": "mailTwo",
    "avatarUrls": {
      "48x48": "https://test.com/secure/useravatar?avatarId=2",
      "24x24": "https://test.com/secure/useravatar?size=small&avatarId=2",
      "16x16": "https://test.com/secure/useravatar?size=xsmall&avatarId=2",
      "32x32": "https://test.com/secure/useravatar?size=medium&avatarId=2"
    },
    "displayName": "displayNameTwo",
    "active": false,
    "timeZone": "Europe",
    "locale": "en_US"
    }
    '''
    
    import json
    
    def parse_response(response):
        response = response.strip()
        json_strs = response.split('n{n"self":')
        json_strs = [json_strs[0]] + ['{n"self":' + s for s in json_strs[1:]]
        json_array_str = '[' + ','.join(json_strs) + ']'
        return json.loads(json_array_str)
    
    parsed_data = parse_response(response)
    
    print(parsed_data)
    
    Login or Signup to reply.
  2. Your data contains multiple JSON objects. They can be separated by detecting a line containing just } and an adjacent line containing just {

    Therefore:

    data = '''
    {
    "self": "https://example1.com",
    "key": "keyOne",
    "name": "nameOne",
    "emailAddress": "mailOne",
    "avatarUrls": {
      "48x48": "https://test.com/secure/useravatar?avatarId=1",
      "24x24": "https://test.com/secure/useravatar?size=small&avatarId=1",
      "16x16": "https://test.com/secure/useravatar?size=xsmall&avatarId=1",
      "32x32": "https://test.com/secure/useravatar?size=medium&avatarId=1"
    },
    "displayName": "displayNameOne",
    "active": true,
    "timeZone": "Europe",
    "locale": "en_UK"
    }
    {
    "self": "https://example2.com",
    "key": "keyTwo",
    "name": "nameTwo",
    "emailAddress": "mailTwo",
    "avatarUrls": {
      "48x48": "https://test.com/secure/useravatar?avatarId=2",
      "24x24": "https://test.com/secure/useravatar?size=small&avatarId=2",
      "16x16": "https://test.com/secure/useravatar?size=xsmall&avatarId=2",
      "32x32": "https://test.com/secure/useravatar?size=medium&avatarId=2"
    },
    "displayName": "displayNameTwo",
    "active": false,
    "timeZone": "Europe",
    "locale": "en_US"
    }
    '''
    from json import loads, dumps
    
    jlist = []
    k = 0
    
    for i, line in enumerate(lines := data.splitlines()):
        if i and line == "{" and lines[i-1] == "}":
            jlist.append(loads("".join(lines[k:i])))
            k = i
    
    jlist.append(loads("".join(lines[k:])))
    
    for j in jlist:
        print(dumps(j, indent=2))
    
    Login or Signup to reply.
  3. Its weird that something like an API responds with something that looks like JSON but isn’t valid JSON.

    Anyways, if the JSON structure follow the same structure, then you know that each item is 16 lines long so just split it on line 16 and you’ll have 2 valid json objects. Something like this:

    import json
    
    data = "" # your json
    
    data_split = data.splitlines()
    json_result = []
    for data in range(0, len(data_split), 16):
        b = "".join(data_split[data:data+16])
        json_result.append(json.loads(b))
    
    print(json_result)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search