teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

jsonのサンプルと、pythonのソースを追加しました。

2019/06/04 06:56

投稿

waku_nagoya
waku_nagoya

スコア200

answer CHANGED
@@ -1,2 +1,95 @@
1
1
  その json データでは、 issues キーが重複するため、どんどん上書きされていって最後の issues のみ残ってしまいます。
2
- そこを改良してください。
2
+ そこを改良してください。
3
+
4
+ ここから追記です。
5
+
6
+ filename : tera.json
7
+ ```json
8
+ {
9
+ "expand": "schemanames",
10
+ "startAt": 0,
11
+ "maxResults": 100,
12
+ "total": 150,
13
+ "issues": [
14
+ {
15
+ "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
16
+ "id": "111111",
17
+ "self": "https://sample.jp//api/2/project/111111&#039",
18
+ "key": "911",
19
+ "fields": {
20
+ "parent": {
21
+ "id": "54321",
22
+ "key": "11",
23
+ "self": "https://sample.jp//api/2/project/11112&#039"
24
+ }
25
+ }
26
+ },
27
+ {
28
+ "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
29
+ "id": "111112",
30
+ "self": "https://sample.jp//api/2/project/111111&#039",
31
+ "key": "912",
32
+ "fields": {
33
+ "parent": {
34
+ "id": "54322",
35
+ "key": "12",
36
+ "self": "https://sample.jp//api/2/project/11112&#039"
37
+ }
38
+ }
39
+ }
40
+ ],
41
+ "issues2": [
42
+ {
43
+ "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
44
+ "id": "222222",
45
+ "self": "https://sample.jp//api/2/project/222222&#039",
46
+ "key": "912",
47
+ "fields": {
48
+ "parent": {
49
+ "id": "64320",
50
+ "key": "21",
51
+ "self": "https://sample.jp//api/2/project/11142&#039"
52
+ }
53
+ }
54
+ },
55
+ {
56
+ "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
57
+ "id": "3333",
58
+ "self": "https://sample.jp//api/2/project/222222&#039",
59
+ "key": "913",
60
+ "fields": {
61
+ "parent": {
62
+ "id": "54321",
63
+ "key": "22",
64
+ "self": "https://sample.jp//api/2/project/11142&#039"
65
+ }
66
+ }
67
+ }
68
+
69
+ ]
70
+ }
71
+
72
+ ```
73
+
74
+ こんな構造のjsonだったとしたら
75
+
76
+ ```python
77
+ import pandas as pd
78
+ from pandas.io.json import json_normalize
79
+ import openpyxl
80
+
81
+ f= open("tera.json", "r")
82
+ data = json.load(f)
83
+
84
+ with pd.ExcelWriter("test.xlsx") as writer:
85
+ for v in data:
86
+ if "issues" in v:
87
+ r = json_normalize(data[v])
88
+ r.to_excel(writer, sheet_name=v)
89
+ ```
90
+
91
+ これで大体やりたいことはできるかと思います。
92
+
93
+ jsonを修正したところとしては、issues のキー名を重複しないように変更したことです。
94
+ もっと構造を変えてもよければ、issues自体をリスト化することです。
95
+ そうするともう少しスマートになるかと。