将条目添加到JSON中

时间:2017-09-01 15:03:27

标签: python json

我正在使用一个API,它在单个调用中没有我需要的所有信息,我需要将它来自的项目代码放入我正在进行的调用中。现在它将项目数据附加到列表中,但我真的需要它作为原始调用的一部分。现在是我的输出:

[{"committer_email": "justin.m.boucher@example.com", "short_id": "981147b9", "title": "Added .gitignore", "author_email": "justin.m.boucher@example.com", "authored_date": "2017-08-29T08:31:11.000-07:00", "created_at": "2017-08-29T08:31:11.000-07:00", "author_name": "Justin Boucher", "parent_ids": [], "committed_date": "2017-08-29T08:31:11.000-07:00", "message": "Added .gitignore\n", "committer_name": "Justin Boucher", "id": "981147b905913a60796283ce10f915c53679df49"}, {"project_id": "2"}]

这是我想要实现的输出:

[{"project_id": "2", "committer_email": "justin.m.boucher@example.com", "short_id": "981147b9", "title": "Added .gitignore", "author_email": "justin.m.boucher@example.com", "authored_date": "2017-08-29T08:31:11.000-07:00", "created_at": "2017-08-29T08:31:11.000-07:00", "author_name": "Justin Boucher", "parent_ids": [], "committed_date": "2017-08-29T08:31:11.000-07:00", "message": "Added .gitignore\n", "committer_name": "Justin Boucher", "id": "981147b905913a60796283ce10f915c53679df49"}]

到目前为止,这是我的代码:

get_commits.py:

import gitlab
import json

gitlab = gitlab.Gitlab()

projects = gitlab.getProjectID()

for i in projects:
    api_str = '/projects/' + str(i) + '/repository/commits'
    connect = gitlab.connectAPI(apiCall=api_str)

    data = json.dumps(connect)

    # Append project id to json, since it isn't created
    # in the commits from Gitlab
    commit = json.loads(data)
    commit.append({'project_id': str(i)})

    # make it pretty again for Splunk to read
    commit = json.dumps(commit)

    print commit

gitlab.py

import os
import ConfigParser
import requests
import json

# Setup Splunk Environment
APPNAME = 'splunk_gitlab'
CONFIG = 'appconfig.conf'
SPLUNK_HOME = os.environ['SPLUNK_HOME']

parser = ConfigParser.SafeConfigParser()


class Gitlab():
    # # Load Settings
    # parser.read(SPLUNK_HOME + '/etc/apps/' + APPNAME + '/local/' + CONFIG)
    # if parser.has_section('Authentication'):
    #     pass
    # else:
    #     parser.read(SPLUNK_HOME + '/etc/apps/' + APPNAME + '/default/' + CONFIG)
    #
    # GITLAB_URL = parser.get('Authentication', 'GITLAB_URL')
    # API_KEY = parser.get('Authentication', 'API_KEY')

    # Used for testing only
    GITLAB_URL = 'http://<my_address>'
    API_KEY = '<my_key>'
    API_SERVER = GITLAB_URL + '/api/v4'



    # Place api call to retrieve data
    def connectAPI(self, apiCall='/projects'):

        headers = {
            'PRIVATE-TOKEN': self.API_KEY
        }

        final_url = self.API_SERVER + apiCall

        resp = requests.get(final_url, headers=headers)

        status_code = resp.status_code

        resp = resp.json()

        if status_code == 200:
            return resp
        else:
            raise Exception("Something went wrong requesting (%s): %s" % (
            resp['errors'][0]['errorType'], resp['errors'][0]['message']))

    def getProjectID(self):
        connect = self.connectAPI(apiCall='/projects')

        data = json.dumps(connect)
        projects = json.loads(data)
        project_list = []
        for i in projects:
            project_list.append(i['id'])

        return project_list

1 个答案:

答案 0 :(得分:0)

如果要将新元素添加到列表中的第一个字典而不是将新字典附加到列表,请尝试使用赋值而不是append

commit[0]['project_id'] = str(i)