前提・実現したいこと
外部のAPIから受け取ったJSONデータを成形して出力したい。
外部のAPIから受け取ったJSON↓を
json
1{ 2 "message": { 3 "listValue": { 4 "values": [ 5 { 6 "structValue": { 7 "fields": { 8 "replyPayload": { 9 "structValue": { 10 "fields": { 11 "title": { 12 "structValue": { 13 "fields": { 14 "format": { 15 "boolValue": false, 16 "kind": "boolValue" 17 }, 18 "text": { 19 "stringValue": "選択", 20 "kind": "stringValue" 21 } 22 } 23 }, 24 "kind": "structValue" 25 }, 26 "comflow": { 27 "structValue": { 28 "fields": { 29 "button": { 30 "listValue": { 31 "values": [ 32 { 33 "structValue": { 34 "fields": { 35 "webview": { 36 "boolValue": false, 37 "kind": "boolValue" 38 }, 39 "label": { 40 "structValue": { 41 "fields": { 42 "format": { 43 "boolValue": false, 44 "kind": "boolValue" 45 }, 46 "text": { 47 "stringValue": "トップに戻る", 48 "kind": "stringValue" 49 } 50 } 51 }, 52 "kind": "structValue" 53 }, 54 "webview": { 55 "boolValue": false, 56 "kind": "boolValue" 57 }, 58 "event": { 59 "structValue": { 60 "fields": { 61 "name": { 62 "stringValue": "eventName1", 63 "kind": "stringValue" 64 } 65 } 66 }, 67 "kind": "structValue" 68 } 69 } 70 }, 71 "kind": "structValue" 72 }, 73 { 74 "structValue": { 75 "fields": { 76 "event": { 77 "structValue": { 78 "fields": { 79 "name": { 80 "stringValue": "eventName2", 81 "kind": "stringValue" 82 } 83 } 84 }, 85 "kind": "structValue" 86 }, 87 "webview": { 88 "boolValue": false, 89 "kind": "boolValue" 90 }, 91 "label": { 92 "structValue": { 93 "fields": { 94 "format": { 95 "boolValue": false, 96 "kind": "boolValue" 97 }, 98 "text": { 99 "stringValue": "前に戻る", 100 "kind": "stringValue" 101 } 102 } 103 }, 104 "kind": "structValue" 105 } 106 } 107 }, 108 "kind": "structValue" 109 } 110 ] 111 }, 112 "kind": "listValue" 113 } 114 } 115 }, 116 "kind": "structValue" 117 } 118 } 119 }, 120 "kind": "structValue" 121 } 122 } 123 }, 124 "kind": "structValue" 125 } 126 ] 127 }, 128 "kind": "listValue" 129 } 130}
このような形↓にしたいのですがうまくいきません。
json
1{ 2 "message": [ 3 { 4 "replyPayload": { 5 "title": { 6 "format": false, 7 "text": "選択" 8 }, 9 "comflow": { 10 "button": [ 11 { 12 "label": { 13 "format": false, 14 "text": "トップに戻る" 15 }, 16 "event": { 17 "name": "eventName1" 18 } 19 }, 20 { 21 "webview": false, 22 "label": { 23 "format": false, 24 "text": "前に戻る" 25 }, 26 "event": { 27 "name": "eventName2" 28 } 29 } 30 ] 31 } 32 } 33 } 34 ] 35}
追記:試したコード・結果
試したコード↓
javascript
1let message = data.payload.fields.message; 2const analysisArr = []; 3analysis(message); 4let payload = {message : analysisArr}; 5console.log(payload); 6 7function analysis(obj) { 8 try { 9 console.log('obj => ' + JSON.stringify(obj)); 10 let box = {}; 11 if (obj.structValue) { 12 box = obj.structValue.fields; 13 Object.keys(box).forEach(function(key) { 14 analysisArr.push({[key] : {}}); 15 console.log('analysisArr => ' + JSON.stringify(analysisArr)); 16 analysis(box[key], key); 17 }); 18 } else if (obj.listValue) { 19 analysisArr.push([]); 20 console.log('analysisArr => ' + JSON.stringify(analysisArr)); 21 box = obj.listValue.values; 22 box.forEach(function(val, key) { 23 analysis(val); 24 }); 25 } else if (obj.stringValue) { 26 const l = analysisArr.length; 27 const parent = analysisArr[l-1]; 28 const parentKey = Object.keys(parent); 29 parent[parentKey[0]] = obj.stringValue; 30 console.log('analysisArr => ' + JSON.stringify(analysisArr)); 31 for (let i = l-2; i > -1; i--) { 32 if (Object.prototype.toString.call(analysisArr[i]) === '[object Object]') { // 自分 33 const cpm = analysisArr[i]; 34 console.log('\ncpm => ' + JSON.stringify(cpm)); 35 const keyArr = Object.keys(cpm); 36 const child = analysisArr[i+1]; 37 if (Object.prototype.toString.call(cpm[keyArr[0]]) === '[object Object]' && !Object.keys(cpm[keyArr[0]]).length) { // 自分のプロパティ 38 if (Object.prototype.toString.call(child) === '[object Array]') { // child 39 cpm[keyArr[0]] = child; 40 } else { 41 const childKey = Object.keys(child); 42 const type = Object.prototype.toString.call(child[childKey[0]]); 43 console.log('type => ' + type); 44 if (type === '[object Object]' || type === '[object String]' || type === '[object Number]' || type === '[object Boolean]') { // childのプロパティ 45 cpm[keyArr[0]] = {[childKey[0]] : child[childKey[0]]}; 46 } else if (Object.prototype.toString.call(child[childKey[0]]) === '[object Array]') { 47 cpm[keyArr[0]] = child; 48 } 49 } 50 } else if (Object.prototype.toString.call(cpm[keyArr[0]]) === '[object Object]' && Object.keys(cpm[keyArr[0]]).length) { 51 if (Object.prototype.toString.call(child) === '[object Array]') { // child 52 cpm[keyArr[0]] = child; 53 //nestCount++; 54 } else { 55 const childKey = Object.keys(child); 56 cpm[childKey[0]] = child[childKey[0]]; 57 //nestCount++; 58 } 59 } else if (Object.prototype.toString.call(cpm[keyArr[0]]) === '[object Array]') { 60 cpm[keyArr[0]].push(child); 61 } 62 analysisArr.pop(); 63 console.log('analysisArr => ' + JSON.stringify(analysisArr)); 64 } else if (Object.prototype.toString.call(analysisArr[i]) === '[object Array]') { 65 const child = analysisArr.pop(); 66 analysisArr[i].push(child); 67 console.log('\nis Array'); 68 console.log('analysisArr => ' + JSON.stringify(analysisArr)); 69 } 70 } 71 } 72 return null; 73 74 } catch(error) { 75 console.log(error); 76 return null; 77 } 78}
結果
{ "message": [ [ { "replyPayload": { "title": { "text": "選択" } } }, { "format": false }, { "comflow": { "button": [ { "label": { "text": "トップに戻る" } } ] } }, { "format": false }, { "event": { "name": "eventName1" } }, { "event": { "name": "eventName2" } }, { "webview": false }, { "label": { "format": false } }, { "text": "前に戻る" } ] ] }
配列のときは listValue:{values:[]},
オブジェクトのときは structValue:{fields:{}},
文字列のときは stringValue:"文字列",
論理型のときは boolValue:論理値,
になっているのはわかったのですがどうすれば下の形にできるかわかりません。
やり方がわかる方いましたら教えてください。
回答2件
あなたの回答
tips
プレビュー