在Python中组合两个json字符串而不会丢失任何数据

时间:2015-04-09 15:57:27

标签: python json

我正在尝试将两个json字符串连接在一起,将共享密钥列表合并在一起。

简单地组合JSON字符串不起作用,因为您有两个有效的JSON字符串,这些字符串构成了无效的JSON字符串。从网上搜索看起来我不得不把它们变成字典并从那里操纵它们。

我在其他一些事情中尝试了以下几点:

   request = getServiceData(service, start_index, max_results) // This returns data from a valid query
   results = request.execute()

   //One attempt
   combined_results = results

   if results.get('nextLink'):
      start_index = str(int(start_index) + int(max_results))
      request = getServiceData(service, start_index, max_results)
      results = request.execute()

      new_combined_results = {}
      for k,v in results.items():
         pairs = zip(v, combined_results[k] + ['']) # add empty to avoid need for zip_longest()
         flat = (item for sub in pairs for item in sub)
         new_combined_results[k] = ''.join(flat)

      combined_results = new_combined_results 

   print json.dumps(combined_results, indent=4)

上面的错误给出:TypeError:强制转换为Unicode:需要字符串或缓冲区,找到列表

我尝试了这样的方法:

def merge(lsta, lstb):
    for i in lstb:
        for j in lsta:
            if j['name'] == i['name']:
                j.update(i)
                break
        else:
            lsta.append(i)

for k,v in dictb.items():
    merge(dicta.setdefault(k, []), v)

这也失败了,最后我试过的另一种方法类似于:

z.copy(x)
z.update(y)

这也不起作用,我无法相信合并本质上是两个dicts或只是J​​SON(如果你使用json.dump)应该很难。

编辑:

根据要求,以下是使用start_index值为1和max_results为1的示例数据:

第1页:

{
    "kind": "analytics#gaData", 
    "rows": [
        [
            "A###", 
            "B###"
        ]
    ], 
    "containsSampledData": true, 
    "columnHeaders": [
        {
            "dataType": "STRING", 
            "columnType": "DIMENSION", 
            "name": "ga:browser"
        }, 
        {
            "dataType": "STRING", 
            "columnType": "DIMENSION", 
            "name": "ga:operatingSystem"
        }
    ], 
    "profileInfo": {
        "webPropertyId": "###", 
        "internalWebPropertyId": "###", 
        "tableId": "ga:###", 
        "profileId": "###", 
        "profileName": "###", 
        "accountId": "###"
    }, 
    "itemsPerPage": 1, 
    "totalsForAllResults": {
        "ga:pageviews": "###", 
        "ga:sessions": "###"
    }, 
    "nextLink": "https://www.googleapis.com/analytics/v3/data/ga?A###", 
    "sampleSize": "###", 
    "query": {
        "max-results": 1, 
        "dimensions": "###", 
        "start-date": "###", 
        "start-index": 1, 
        "ids": "ga:###", 
        "metrics": [
            "ga:sessions", 
            "ga:pageviews"
        ], 
        "end-date": "###"
    }, 
    "totalResults": ###, 
    "id": "https://www.googleapis.com/analytics/v3/data/gaA###", 
    "selfLink": "https://www.googleapis.com/analytics/v3/data/gaA###", 
    "sampleSpace": "###"
}

第2页:

{
    "kind": "analytics#gaData", 
    "rows": [
        [
            "1###", 
            "2###"
        ]
    ], 
    "containsSampledData": true, 
    "columnHeaders": [
        {
            "dataType": "STRING", 
            "columnType": "DIMENSION", 
            "name": "ga:browser"
        }, 
        {
            "dataType": "STRING", 
            "columnType": "DIMENSION", 
            "name": "ga:operatingSystem"
        }
    ], 
    "profileInfo": {
        "webPropertyId": "###", 
        "internalWebPropertyId": "###", 
        "tableId": "ga:###", 
        "profileId": "###", 
        "profileName": "###", 
        "accountId": "###"
    }, 
    "itemsPerPage": 1, 
    "totalsForAllResults": {
        "ga:pageviews": "###", 
        "ga:sessions": "###"
    }, 
    "nextLink": "https://www.googleapis.com/analytics/v3/data/ga?1###", 
    "sampleSize": "###", 
    "query": {
        "max-results": 1, 
        "dimensions": "###", 
        "start-date": "###", 
        "start-index": 1, 
        "ids": "ga:###", 
        "metrics": [
            "ga:sessions", 
            "ga:pageviews"
        ], 
        "end-date": "###"
    }, 
    "totalResults": ###, 
    "id": "https://www.googleapis.com/analytics/v3/data/ga1###", 
    "selfLink": "https://www.googleapis.com/analytics/v3/data/ga1###", 
    "sampleSpace": "###"
}

组合:

{
    "kind": "analytics#gaData", 
    "rows": [
        [
            "A###", 
            "B###"
        ],
        [
            "1###", 
            "2###"
        ]
    ], 
    "containsSampledData": true, 
    "columnHeaders": [
        {
            "dataType": "STRING", 
            "columnType": "DIMENSION", 
            "name": "ga:browser"
        }, 
        {
            "dataType": "STRING", 
            "columnType": "DIMENSION", 
            "name": "ga:operatingSystem"
        }
    ], 
    "profileInfo": {
        "webPropertyId": "###", 
        "internalWebPropertyId": "###", 
        "tableId": "ga:###", 
        "profileId": "###", 
        "profileName": "###", 
        "accountId": "###"
    }, 
    "itemsPerPage": 1, 
    "totalsForAllResults": {
        "ga:pageviews": "###", 
        "ga:sessions": "###"
    }, 
    "nextLink": {
        "https://www.googleapis.com/analytics/v3/data/ga?A###",
        "https://www.googleapis.com/analytics/v3/data/ga?1###"
    },
    "sampleSize": "###", 
    "query": {
        "max-results": {
            ###, 
            ###
        }
        "dimensions": "###", 
        "start-date": "###", 
        "start-index": {
            ###, 
            ###
        }
        "ids": "ga:###", 
        "metrics": [
            "ga:sessions", 
            "ga:pageviews"
        ], 
        "end-date": "###"
    }, 
    "totalResults": ###, 
    "id": {
        "https://www.googleapis.com/analytics/v3/data/gaA###", 
        "https://www.googleapis.com/analytics/v3/data/ga1###", 
    }
    "selfLink": {
        "https://www.googleapis.com/analytics/v3/data/gaA###", 
        "https://www.googleapis.com/analytics/v3/data/ga1###", 
    }
    "sampleSpace": "###"
}

或类似的......

0 个答案:

没有答案