0

Zendesk からデータを取得するための API 呼び出しを行う Python スクリプトがあります。(Python 3.x を使用) JSON オブジェクトの構造は次のようになります。

{
  "id":               35436,
  "url":              "https://company.zendesk.com/api/v2/tickets/35436.json",
  "external_id":      "ahg35h3jh",
  "created_at":       "2009-07-20T22:55:29Z",
  "updated_at":       "2011-05-05T10:38:52Z",
  "type":             "incident",
  "subject":          "Help, my printer is on fire!",
  "raw_subject":      "{{dc.printer_on_fire}}",
  "description":      "The fire is very colorful.",
  "priority":         "high",
  "status":           "open",
  "recipient":        "support@company.com",
  "requester_id":     20978392,
  "submitter_id":     76872,
  "assignee_id":      235323,
  "organization_id":  509974,
  "group_id":         98738,
  "collaborator_ids": [35334, 234],
  "forum_topic_id":   72648221,
  "problem_id":       9873764,
  "has_incidents":    false,
  "due_at":           null,
  "tags":             ["enterprise", "other_tag"],
  "via": {
    "channel": "web"
  },
  "custom_fields": [
    {
      "id":    27642,
      "value": "745"
    },
    {
      "id":    27648,
      "value": "yes"
    }
  ],
  "satisfaction_rating": {
    "id": 1234,
    "score": "good",
    "comment": "Great support!"
  },
  "sharing_agreement_ids": [84432]
}

問題が発生している場所は、"custom_fields"具体的にはセクションにあります。値が必要な各チケット内に特定のカスタム フィールドがあり、その特定の値のみが必要です。

Python コードの多くの詳細を省くために、以下の各チケットの各値を読み取り、それを出力変数に追加してから、その出力変数を .csv に書き込みます。破損が発生している特定の場所は次のとおりです。

output += str(ticket['custom_fields'][id:23825198]).replace(',', '')+',' 

すべてのナンセンスな置き換えは、コンマ区切りのファイルに入るため、値内のコンマが削除されていることを確認することです。とにかく、ここに私が得ているエラーがあります:

    output += str(ticket['custom_fields'][id:int(23825198)]).replace(',', '')+','
TypeError: slice indices must be integers or None or have an __index__ method

ご覧のとおり、問題を解決するためにこれのいくつかの異なるバリエーションを試しましたが、まだ修正を見つけていません. 私はいくつかの助けを使うことができます!

ありがとう...

4

1 に答える 1

0

json.loads() を使用していますか? その場合は、キーを取得し、キーに対して if ステートメントを実行できます。キーとそれぞれの値を取得する方法の例を以下に示します。

import json

some_json = """{
"id":               35436,
"url":              "https://company.zendesk.com/api/v2/tickets/35436.json",
"external_id":      "ahg35h3jh",
"created_at":       "2009-07-20T22:55:29Z",
"updated_at":       "2011-05-05T10:38:52Z",
"type":             "incident",
"subject":          "Help, my printer is on fire!",
"raw_subject":      "{{dc.printer_on_fire}}",
"description":      "The fire is very colorful.",
"priority":         "high",
"status":           "open",
"recipient":        "support@company.com",
"requester_id":     20978392,
"submitter_id":     76872,
"assignee_id":      235323,
"organization_id":  509974,
"group_id":         98738,
"collaborator_ids": [35334, 234],
"forum_topic_id":   72648221,
"problem_id":       9873764,
"has_incidents":    false,
"due_at":           null,
"tags":             ["enterprise", "other_tag"],
"via": {
    "channel": "web"
},
"custom_fields": [
    {
    "sid":    27642,
    "value": "745"
    },
    {
    "id":    27648,
    "value": "yes"
    }
],
"satisfaction_rating": {
    "id": 1234,
    "score": "good",
    "comment": "Great support!"
},
"sharing_agreement_ids": [84432]
}"""

# load the json object
zenJSONObj = json.loads(some_json)

# Shows a list of all custom fields
print("All the custom field data")
print(zenJSONObj['custom_fields'])
print("----")
# Tells you all the keys in the custom_fields
print("How keys and the values")
for custom_field in zenJSONObj['custom_fields']:
    print("----")
    for key in custom_field.keys():
        print("key:",key," value: ",custom_field[key])

次に、次のようにして JSON オブジェクトを変更できます。

print(zenJSONObj['custom_fields'][0])
zenJSONObj['custom_fields'][0]['value'] = 'something new'
print(zenJSONObj['custom_fields'][0])

次に、次を使用して再エンコードします。

newJSONObject = json.dumps(zenJSONObj, sort_keys=True, indent=4)

これが何らかの助けになることを願っています。

于 2014-11-25T22:14:38.920 に答える