JSON formation from a JSON data

Tags: , , , ,



I have the JSON data below. I need the JSON to split and form a new JSON data object. I’m new to this and am struggling to get a way to solve this.

below JSON data:

[
   {
      "timestamp":"2020-11-13T20:57:17.611Z",
      "session_id":"eed8d600-0438-37d3-dd96-25081a91b90a",
      "Telephone":"555555555",
      "Name":"Mike",
      "Thread":[
         {
            "textPayload":{
               "timestamp":"2020-11-13T20:57:08.552Z",
               "id":"a97809e8-3466-4404-947e-4ec89f96878b-aabf4fdf",
               "status":{
                  "error_type":"success",
                  "code":200
               },
               "lang":"en",
               "session_id":"eed8d600-0438-37d3-dd96-25081a91b90a",
               "result":{
                  "score":1,
                  "action_incomplete":true,
                  "resolved_query":"hi",
                  "action":"input.welcome",
                  "source":"agent",
                  "metadata":{
                     "intent_name":"Default Welcome Intent",
                     "webhook_used":false,
                     "intent_id":"d06d9b61-eee2-4481-a038-bc97194d611a",
                     "is_fallback_intent":false,
                     "webhook_for_slot_filling_used":false
                  },
                  "parameters":{
                     "fields":{
                        "value":{
                           "string_value":""
                        },
                        "key":"name"
                     }
                  },
                  "contexts":{
                     "parameters":{
                        "fields":{
                           "key":"no-match",
                           "value":{
                              "number_value":0
                           }
                        }
                     },
                     "name":"__system_counters__",
                     "lifespan":1
                  },
                  "fulfillment":{
                     "messages":{
                        "type":{
                           "number_value":0
                        },
                        "speech":{
                           "string_value":"What is the name?"
                        }
                     },
                     "speech":"What is the name?"
                  }
               }
            },
            "insertId":"1p1x1wdfgkwf2a",
            "severity":"INFO",
            "receiveTimestamp":"2020-11-13T20:57:08.592891686Z",
            "trace":"eed8d600-0438-37d3-dd96-25081a91b90a",
            "labels":{
               "request_id":"a97809e8-3466-4404-947e-4ec89f96878b-aabf4fdf",
               "type":"dialogflow_response",
               "protocol":"V2_BETA1",
               "source":"DIALOGFLOW_CONSOLE"
            },
            "timestamp":"2020-11-13T20:57:08.571Z",
            "logName":"projects/entuber/logs/dialogflow_agent",
            "resource":{
               "type":"global",
               "labels":{
                  "project_id":"entuber"
               }
            },
            "session_id":"eed8d600-0438-37d3-dd96-25081a91b90a"
         },
         {
            "timestamp":"2020-11-13T20:57:11.746Z",
            "labels":{
               "type":"dialogflow_response",
               "protocol":"V2_BETA1",
               "source":"DIALOGFLOW_CONSOLE",
               "request_id":"70b25970-e228-41b9-9898-26d68bb67c13-aabf4fdf"
            },
            "resource":{
               "labels":{
                  "project_id":"entuber"
               },
               "type":"global"
            },
            "logName":"projects/entuber/logs/dialogflow_agent",
            "receiveTimestamp":"2020-11-13T20:57:11.766438485Z",
            "trace":"eed8d600-0438-37d3-dd96-25081a91b90a",
            "severity":"INFO",
            "session_id":"eed8d600-0438-37d3-dd96-25081a91b90a",
            "textPayload":{
               "session_id":"eed8d600-0438-37d3-dd96-25081a91b90a",
               "status":{
                  "code":200,
                  "error_type":"success"
               },
               "timestamp":"2020-11-13T20:57:11.728Z",
               "id":"70b25970-e228-41b9-9898-26d68bb67c13-aabf4fdf",
               "lang":"en",
               "result":{
                  "contexts":{
                     "lifespan":50,
                     "parameters":{
                        "fields":{
                           "value":{
                              "string_value":"sam"
                           },
                           "key":"name.original"
                        }
                     },
                     "name":"name"
                  },
                  "action":"input.welcome",
                  "resolved_query":"sam",
                  "fulfillment":{
                     "speech":"Hi, I'm Tara. I've been modified to tell you what your bills are. Ask me for your current bill and I'll tell you how much you have to pay and when it's due. Make sure you know the account number though.",
                     "messages":{
                        "speech":{
                           "string_value":""
                        },
                        "lang":"en",
                        "type":{
                           "number_value":0
                        }
                     }
                  },
                  "parameters":{
                     "fields":{
                        "key":"name",
                        "value":{
                           "string_value":"Sam"
                        }
                     }
                  },
                  "metadata":{
                     "intent_name":"Default Welcome Intent",
                     "is_fallback_intent":false,
                     "intent_id":"d06d9b61-eee2-4481-a038-bc97194d611a",
                     "webhook_for_slot_filling_used":false,
                     "webhook_used":false
                  },
                  "source":"agent",
                  "score":1
               }
            },
            "insertId":"1orjxuhfg78gle"
         },
      ]
   },
   {
      "session_id":"TestTaraSession",
      "timestamp":"2020-11-16T06:17:37.536Z",
      "Thread":[
         {
            "resource":{
               "type":"global",
               "labels":{
                  "project_id":"entuber"
               }
            },
            "insertId":"ftk71rg38zc3de",
            "trace":"TestTaraSession",
            "logName":"projects/entuber/logs/dialogflow_agent",
            "labels":{
               "type":"dialogflow_response",
               "request_id":"1fd35e1d-7830-4606-937d-b8685dde0129-aabf4fdf",
               "protocol":"V2_BETA1"
            },
            "severity":"INFO",
            "timestamp":"2020-11-16T06:16:50.421Z",
            "session_id":"TestTaraSession",
            "receiveTimestamp":"2020-11-16T06:16:50.45315191Z",
            "textPayload":{
               "status":{
                  "error_type":"success",
                  "code":200
               },
               "id":"1fd35e1d-7830-4606-937d-b8685dde0129-aabf4fdf",
               "lang":"en",
               "session_id":"TestTaraSession",
               "result":{
                  "score":1,
                  "parameters":{
                     "fields":{
                        "value":{
                           "string_value":""
                        },
                        "key":"name"
                     }
                  },
                  "metadata":{
                     "intent_id":"d06d9b61-eee2-4481-a038-bc97194d611a",
                     "is_fallback_intent":false,
                     "webhook_used":false,
                     "intent_name":"Default Welcome Intent",
                     "webhook_for_slot_filling_used":false
                  },
                  "action":"input.welcome",
                  "contexts":{
                     "lifespan":1,
                     "name":"__system_counters__",
                     "parameters":{
                        "fields":{
                           "value":{
                              "number_value":0
                           },
                           "key":"no-match"
                        }
                     }
                  },
                  "action_incomplete":true,
                  "fulfillment":{
                     "messages":{
                        "type":{
                           "number_value":0
                        },
                        "speech":{
                           "string_value":"What is the name?"
                        }
                     },
                     "speech":"What is the name?"
                  },
                  "source":"agent",
                  "resolved_query":"WELCOME"
               },
               "timestamp":"2020-11-16T06:16:50.408Z"
            }
         },
         {
            "session_id":"TestTaraSession",
            "timestamp":"2020-11-16T06:17:08.746Z",
            "insertId":"1ggf79rg3d2lksr",
            "labels":{
               "protocol":"V2_BETA1",
               "type":"dialogflow_response",
               "request_id":"ff18d72b-5fbc-4e44-a52d-f987d0d59d9d-aabf4fdf"
            },
            "logName":"projects/entuber/logs/dialogflow_agent",
            "receiveTimestamp":"2020-11-16T06:17:08.756453598Z",
            "trace":"TestTaraSession",
            "textPayload":{
               "session_id":"TestTaraSession",
               "lang":"en",
               "result":{
                  "contexts":{
                     "parameters":{
                        "fields":{
                           "value":{
                              "string_value":"Tara"
                           },
                           "key":"name.original"
                        }
                     },
                     "lifespan":50,
                     "name":"name"
                  },
                  "metadata":{
                     "intent_id":"d06d9b61-eee2-4481-a038-bc97194d611a",
                     "is_fallback_intent":false,
                     "webhook_used":false,
                     "webhook_for_slot_filling_used":false,
                     "intent_name":"Default Welcome Intent"
                  },
                  "parameters":{
                     "fields":{
                        "value":{
                           "string_value":"Tara"
                        },
                        "key":"name"
                     }
                  },
                  "action":"input.welcome",
                  "source":"agent",
                  "score":1,
                  "fulfillment":{
                     "messages":{
                        "lang":"en",
                        "speech":{
                           "string_value":""
                        },
                        "type":{
                           "number_value":0
                        }
                     },
                     "speech":"Hi, I'm Tara. I've been modified to tell you what your bills are. Ask me for your current bill and I'll tell you how much you have to pay and when it's due. Make sure you know the account number though."
                  },
                  "resolved_query":"Tara"
               },
               "timestamp":"2020-11-16T06:17:08.742Z",
               "status":{
                  "error_type":"success",
                  "code":200
               },
               "id":"ff18d72b-5fbc-4e44-a52d-f987d0d59d9d-aabf4fdf"
            },
            "severity":"INFO",
            "resource":{
               "labels":{
                  "project_id":"entuber"
               },
               "type":"global"
            }
         },
         {
            "trace":"TestTaraSession",
            "receiveTimestamp":"2020-11-16T06:17:17.973535789Z",
            "logName":"projects/entuber/logs/dialogflow_agent",
            "textPayload":{
               "lang":"en",
               "timestamp":"2020-11-16T06:17:17.943Z",
               "result":{
                  "score":0.686966,
                  "parameters":{
                     "fields":{
                        "value":{
                           "string_value":""
                        },
                        "key":"accountNo"
                     }
                  },
                  "action":"com.tara.bills",
                  "metadata":{
                     "intent_id":"5ff968ab-3646-4abe-ae7e-93203997e5fb",
                     "webhook_used":true,
                     "webhook_for_slot_filling_used":false,
                     "intent_name":"Read Bill",
                     "is_fallback_intent":false
                  },
                  "resolved_query":"Bill details",
                  "action_incomplete":true,
                  "source":"agent",
                  "fulfillment":{
                     "messages":{
                        "speech":{
                           "string_value":"What is your account number?"
                        },
                        "type":{
                           "number_value":0
                        }
                     },
                     "speech":"What is your account number?"
                  },
                  "contexts":{
                     "parameters":{
                        "fields":{
                           "key":"no-match",
                           "value":{
                              "number_value":0
                           }
                        }
                     },
                     "name":"__system_counters__",
                     "lifespan":1
                  }
               },
               "id":"ab128f85-0044-42ba-ae8a-c77d0905d972-aabf4fdf",
               "status":{
                  "error_type":"success",
                  "code":200
               },
               "session_id":"TestTaraSession"
            },
            "severity":"INFO",
            "session_id":"TestTaraSession",
            "resource":{
               "labels":{
                  "project_id":"entuber"
               },
               "type":"global"
            },
            "timestamp":"2020-11-16T06:17:17.965Z",
            "insertId":"dc02v6g3eo2vya",
            "labels":{
               "type":"dialogflow_response",
               "protocol":"V2_BETA1",
               "request_id":"ab128f85-0044-42ba-ae8a-c77d0905d972-aabf4fdf"
            }
         },
         {
            "textPayload":{
               "session_id":"TestTaraSession",
               "status":{
                  "error_type":"success",
                  "code":200
               },
               "result":{
                  "metadata":{
                     "webhook_used":false,
                     "intent_name":"Read Bill",
                     "webhook_response_time":3334,
                     "intent_id":"5ff968ab-3646-4abe-ae7e-93203997e5fb",
                     "is_fallback_intent":false,
                     "webhook_for_slot_filling_used":false
                  },
                  "contexts":{
                     "name":"defaultwelcomeintent-followup",
                     "lifespan":1,
                     "parameters":{
                        "fields":{
                           "value":{
                              "string_value":"Tara"
                           },
                           "key":"name.original"
                        }
                     }
                  },
                  "source":"agent",
                  "action":"com.tara.bills",
                  "fulfillment":{
                     "speech":"Your current bill of $2927.62 is due on 10/19/2020.",
                     "messages":{
                        "speech":{
                           "string_value":"Your current bill of $2927.62 is due on 10/19/2020."
                        },
                        "type":{
                           "number_value":0
                        }
                     }
                  },
                  "score":1,
                  "parameters":{
                     "fields":{
                        "value":{
                           "number_value":21012875
                        },
                        "key":"accountNo"
                     }
                  },
                  "resolved_query":21012875
               },
               "lang":"en",
               "id":"1d3df1d2-1753-48fc-948e-7fc91473d055-aabf4fdf",
               "timestamp":"2020-11-16T06:17:34.184Z"
            },
            "insertId":"1def6i1g15mb48l",
            "labels":{
               "protocol":"V2_BETA1",
               "request_id":"1d3df1d2-1753-48fc-948e-7fc91473d055-aabf4fdf",
               "type":"dialogflow_response"
            },
            "receiveTimestamp":"2020-11-16T06:17:37.548546643Z",
            "timestamp":"2020-11-16T06:17:37.536Z",
            "severity":"INFO",
            "resource":{
               "type":"global",
               "labels":{
                  "project_id":"entuber"
               }
            },
            "session_id":"TestTaraSession",
            "trace":"TestTaraSession",
            "logName":"projects/entuber/logs/dialogflow_agent"
         }
      ]
   },
   {
      "timestamp":"2020-11-16T06:29:14.564Z",
      "session_id":"9b86412b-ec2d-39c0-7cbc-7a44a74a8804",
      "Thread":[
         {
            "timestamp":"2020-11-16T06:29:01.638Z",
            "session_id":"9b86412b-ec2d-39c0-7cbc-7a44a74a8804",
            "logName":"projects/entuber/logs/dialogflow_agent",
            "severity":"INFO",
            "insertId":"1httvn0g2nj3fae",
            "labels":{
               "request_id":"94ec1661-d472-4ef0-a7bd-d85e22062e4d-aabf4fdf",
               "protocol":"V2_BETA1",
               "type":"dialogflow_response",
               "source":"DIALOGFLOW_CONSOLE"
            },
            "trace":"9b86412b-ec2d-39c0-7cbc-7a44a74a8804",
            "resource":{
               "labels":{
                  "project_id":"entuber"
               },
               "type":"global"
            },
            "textPayload":{
               "status":{
                  "error_type":"success",
                  "code":200
               },
               "session_id":"9b86412b-ec2d-39c0-7cbc-7a44a74a8804",
               "result":{
                  "resolved_query":"hello",
                  "contexts":{
                     "parameters":{
                        "fields":{
                           "key":"no-match",
                           "value":{
                              "number_value":0
                           }
                        }
                     },
                     "lifespan":1,
                     "name":"__system_counters__"
                  },
                  "score":1,
                  "action_incomplete":true,
                  "fulfillment":{
                     "messages":{
                        "speech":{
                           "string_value":"What is the name?"
                        },
                        "type":{
                           "number_value":0
                        }
                     },
                     "speech":"What is the name?"
                  },
                  "action":"input.welcome",
                  "metadata":{
                     "is_fallback_intent":false,
                     "intent_id":"d06d9b61-eee2-4481-a038-bc97194d611a",
                     "webhook_for_slot_filling_used":false,
                     "webhook_used":false,
                     "intent_name":"Default Welcome Intent"
                  },
                  "parameters":{
                     "fields":{
                        "key":"name",
                        "value":{
                           "string_value":""
                        }
                     }
                  },
                  "source":"agent"
               },
               "id":"94ec1661-d472-4ef0-a7bd-d85e22062e4d-aabf4fdf",
               "timestamp":"2020-11-16T06:29:01.596Z",
               "lang":"en"
            },
            "receiveTimestamp":"2020-11-16T06:29:01.64859801Z"
         },
         {
            "severity":"INFO",
            "receiveTimestamp":"2020-11-16T06:29:07.676213264Z",
            "resource":{
               "type":"global",
               "labels":{
                  "project_id":"entuber"
               }
            },
            "insertId":"13ojz2yg2npq0nk",
            "labels":{
               "source":"DIALOGFLOW_CONSOLE",
               "type":"dialogflow_response",
               "request_id":"896b6d41-fe8b-4123-ba68-07004435db42-aabf4fdf",
               "protocol":"V2_BETA1"
            },
            "timestamp":"2020-11-16T06:29:07.667Z",
            "logName":"projects/entuber/logs/dialogflow_agent",
            "trace":"9b86412b-ec2d-39c0-7cbc-7a44a74a8804",
            "textPayload":{
               "status":{
                  "error_type":"success",
                  "code":200
               },
               "timestamp":"2020-11-16T06:29:07.62Z",
               "lang":"en",
               "result":{
                  "contexts":{
                     "lifespan":50,
                     "name":"name",
                     "parameters":{
                        "fields":{
                           "key":"name.original",
                           "value":{
                              "string_value":"tara"
                           }
                        }
                     }
                  },
                  "source":"agent",
                  "resolved_query":"tara",
                  "score":1,
                  "action":"input.welcome",
                  "fulfillment":{
                     "messages":{
                        "lang":"en",
                        "speech":{
                           "string_value":""
                        },
                        "type":{
                           "number_value":0
                        }
                     },
                     "speech":"Can I have your name?"
                  },
                  "parameters":{
                     "fields":{
                        "key":"name",
                        "value":{
                           "string_value":"Tara"
                        }
                     }
                  },
                  "metadata":{
                     "webhook_for_slot_filling_used":false,
                     "is_fallback_intent":false,
                     "intent_id":"d06d9b61-eee2-4481-a038-bc97194d611a",
                     "intent_name":"Default Welcome Intent",
                     "webhook_used":false
                  }
               },
               "id":"896b6d41-fe8b-4123-ba68-07004435db42-aabf4fdf",
               "session_id":"9b86412b-ec2d-39c0-7cbc-7a44a74a8804"
            },
            "session_id":"9b86412b-ec2d-39c0-7cbc-7a44a74a8804"
         }
      ]
   }
]

This has to be done for all the session_id values in the input JSON data. It has to form all the JSON data to iterate and show in another HTML. I’m confused here and aim to get the following: Sample output:

[
   {
      "session_id":"eed8d600-0438-37d3-dd96-25081a91b90a",
      "timestamp":"2020-11-13T20:57:17.611Z",
      "labels":{
         "request_id":"a97809e8-3466-4404-947e-4ec89f96878b-aabf4fdf",
         "type":"dialogflow_response",
         "protocol":"V2_BETA1",
         "source":"DIALOGFLOW_CONSOLE"
      },
      "Thread":[
         {
            "speech":"What is the name?",
            "timestamp":"2020-11-13T20:57:08.552Z",
            "id":"a97809e8-3466-4404-947e-4ec89f96878b-aabf4fdf"
         }
      ]
   }
]

I need to get a result like above for all session_id values.

Answer

You have a concept-level problem. A session may have multiple threads for separate requests and therefore you need to support a one-to-many relationship for session-request/thread. So, my solution is not exactly looking like your sample output, because there are multiple threads, each having their own label:

var input = [
   {
      "timestamp":"2020-11-13T20:57:17.611Z",
      "session_id":"eed8d600-0438-37d3-dd96-25081a91b90a",
      "Telephone":"555555555",
      "Name":"Mike",
      "Thread":[
         {
            "textPayload":{
               "timestamp":"2020-11-13T20:57:08.552Z",
               "id":"a97809e8-3466-4404-947e-4ec89f96878b-aabf4fdf",
               "status":{
                  "error_type":"success",
                  "code":200
               },
               "lang":"en",
               "session_id":"eed8d600-0438-37d3-dd96-25081a91b90a",
               "result":{
                  "score":1,
                  "action_incomplete":true,
                  "resolved_query":"hi",
                  "action":"input.welcome",
                  "source":"agent",
                  "metadata":{
                     "intent_name":"Default Welcome Intent",
                     "webhook_used":false,
                     "intent_id":"d06d9b61-eee2-4481-a038-bc97194d611a",
                     "is_fallback_intent":false,
                     "webhook_for_slot_filling_used":false
                  },
                  "parameters":{
                     "fields":{
                        "value":{
                           "string_value":""
                        },
                        "key":"name"
                     }
                  },
                  "contexts":{
                     "parameters":{
                        "fields":{
                           "key":"no-match",
                           "value":{
                              "number_value":0
                           }
                        }
                     },
                     "name":"__system_counters__",
                     "lifespan":1
                  },
                  "fulfillment":{
                     "messages":{
                        "type":{
                           "number_value":0
                        },
                        "speech":{
                           "string_value":"What is the name?"
                        }
                     },
                     "speech":"What is the name?"
                  }
               }
            },
            "insertId":"1p1x1wdfgkwf2a",
            "severity":"INFO",
            "receiveTimestamp":"2020-11-13T20:57:08.592891686Z",
            "trace":"eed8d600-0438-37d3-dd96-25081a91b90a",
            "labels":{
               "request_id":"a97809e8-3466-4404-947e-4ec89f96878b-aabf4fdf",
               "type":"dialogflow_response",
               "protocol":"V2_BETA1",
               "source":"DIALOGFLOW_CONSOLE"
            },
            "timestamp":"2020-11-13T20:57:08.571Z",
            "logName":"projects/entuber/logs/dialogflow_agent",
            "resource":{
               "type":"global",
               "labels":{
                  "project_id":"entuber"
               }
            },
            "session_id":"eed8d600-0438-37d3-dd96-25081a91b90a"
         },
         {
            "timestamp":"2020-11-13T20:57:11.746Z",
            "labels":{
               "type":"dialogflow_response",
               "protocol":"V2_BETA1",
               "source":"DIALOGFLOW_CONSOLE",
               "request_id":"70b25970-e228-41b9-9898-26d68bb67c13-aabf4fdf"
            },
            "resource":{
               "labels":{
                  "project_id":"entuber"
               },
               "type":"global"
            },
            "logName":"projects/entuber/logs/dialogflow_agent",
            "receiveTimestamp":"2020-11-13T20:57:11.766438485Z",
            "trace":"eed8d600-0438-37d3-dd96-25081a91b90a",
            "severity":"INFO",
            "session_id":"eed8d600-0438-37d3-dd96-25081a91b90a",
            "textPayload":{
               "session_id":"eed8d600-0438-37d3-dd96-25081a91b90a",
               "status":{
                  "code":200,
                  "error_type":"success"
               },
               "timestamp":"2020-11-13T20:57:11.728Z",
               "id":"70b25970-e228-41b9-9898-26d68bb67c13-aabf4fdf",
               "lang":"en",
               "result":{
                  "contexts":{
                     "lifespan":50,
                     "parameters":{
                        "fields":{
                           "value":{
                              "string_value":"sam"
                           },
                           "key":"name.original"
                        }
                     },
                     "name":"name"
                  },
                  "action":"input.welcome",
                  "resolved_query":"sam",
                  "fulfillment":{
                     "speech":"Hi, I'm Tara. I've been modified to tell you what your bills are. Ask me for your current bill and I'll tell you how much you have to pay and when it's due. Make sure you know the account number though.",
                     "messages":{
                        "speech":{
                           "string_value":""
                        },
                        "lang":"en",
                        "type":{
                           "number_value":0
                        }
                     }
                  },
                  "parameters":{
                     "fields":{
                        "key":"name",
                        "value":{
                           "string_value":"Sam"
                        }
                     }
                  },
                  "metadata":{
                     "intent_name":"Default Welcome Intent",
                     "is_fallback_intent":false,
                     "intent_id":"d06d9b61-eee2-4481-a038-bc97194d611a",
                     "webhook_for_slot_filling_used":false,
                     "webhook_used":false
                  },
                  "source":"agent",
                  "score":1
               }
            },
            "insertId":"1orjxuhfg78gle"
         },
      ]
   },
   {
      "session_id":"TestTaraSession",
      "timestamp":"2020-11-16T06:17:37.536Z",
      "Thread":[
         {
            "resource":{
               "type":"global",
               "labels":{
                  "project_id":"entuber"
               }
            },
            "insertId":"ftk71rg38zc3de",
            "trace":"TestTaraSession",
            "logName":"projects/entuber/logs/dialogflow_agent",
            "labels":{
               "type":"dialogflow_response",
               "request_id":"1fd35e1d-7830-4606-937d-b8685dde0129-aabf4fdf",
               "protocol":"V2_BETA1"
            },
            "severity":"INFO",
            "timestamp":"2020-11-16T06:16:50.421Z",
            "session_id":"TestTaraSession",
            "receiveTimestamp":"2020-11-16T06:16:50.45315191Z",
            "textPayload":{
               "status":{
                  "error_type":"success",
                  "code":200
               },
               "id":"1fd35e1d-7830-4606-937d-b8685dde0129-aabf4fdf",
               "lang":"en",
               "session_id":"TestTaraSession",
               "result":{
                  "score":1,
                  "parameters":{
                     "fields":{
                        "value":{
                           "string_value":""
                        },
                        "key":"name"
                     }
                  },
                  "metadata":{
                     "intent_id":"d06d9b61-eee2-4481-a038-bc97194d611a",
                     "is_fallback_intent":false,
                     "webhook_used":false,
                     "intent_name":"Default Welcome Intent",
                     "webhook_for_slot_filling_used":false
                  },
                  "action":"input.welcome",
                  "contexts":{
                     "lifespan":1,
                     "name":"__system_counters__",
                     "parameters":{
                        "fields":{
                           "value":{
                              "number_value":0
                           },
                           "key":"no-match"
                        }
                     }
                  },
                  "action_incomplete":true,
                  "fulfillment":{
                     "messages":{
                        "type":{
                           "number_value":0
                        },
                        "speech":{
                           "string_value":"What is the name?"
                        }
                     },
                     "speech":"What is the name?"
                  },
                  "source":"agent",
                  "resolved_query":"WELCOME"
               },
               "timestamp":"2020-11-16T06:16:50.408Z"
            }
         },
         {
            "session_id":"TestTaraSession",
            "timestamp":"2020-11-16T06:17:08.746Z",
            "insertId":"1ggf79rg3d2lksr",
            "labels":{
               "protocol":"V2_BETA1",
               "type":"dialogflow_response",
               "request_id":"ff18d72b-5fbc-4e44-a52d-f987d0d59d9d-aabf4fdf"
            },
            "logName":"projects/entuber/logs/dialogflow_agent",
            "receiveTimestamp":"2020-11-16T06:17:08.756453598Z",
            "trace":"TestTaraSession",
            "textPayload":{
               "session_id":"TestTaraSession",
               "lang":"en",
               "result":{
                  "contexts":{
                     "parameters":{
                        "fields":{
                           "value":{
                              "string_value":"Tara"
                           },
                           "key":"name.original"
                        }
                     },
                     "lifespan":50,
                     "name":"name"
                  },
                  "metadata":{
                     "intent_id":"d06d9b61-eee2-4481-a038-bc97194d611a",
                     "is_fallback_intent":false,
                     "webhook_used":false,
                     "webhook_for_slot_filling_used":false,
                     "intent_name":"Default Welcome Intent"
                  },
                  "parameters":{
                     "fields":{
                        "value":{
                           "string_value":"Tara"
                        },
                        "key":"name"
                     }
                  },
                  "action":"input.welcome",
                  "source":"agent",
                  "score":1,
                  "fulfillment":{
                     "messages":{
                        "lang":"en",
                        "speech":{
                           "string_value":""
                        },
                        "type":{
                           "number_value":0
                        }
                     },
                     "speech":"Hi, I'm Tara. I've been modified to tell you what your bills are. Ask me for your current bill and I'll tell you how much you have to pay and when it's due. Make sure you know the account number though."
                  },
                  "resolved_query":"Tara"
               },
               "timestamp":"2020-11-16T06:17:08.742Z",
               "status":{
                  "error_type":"success",
                  "code":200
               },
               "id":"ff18d72b-5fbc-4e44-a52d-f987d0d59d9d-aabf4fdf"
            },
            "severity":"INFO",
            "resource":{
               "labels":{
                  "project_id":"entuber"
               },
               "type":"global"
            }
         },
         {
            "trace":"TestTaraSession",
            "receiveTimestamp":"2020-11-16T06:17:17.973535789Z",
            "logName":"projects/entuber/logs/dialogflow_agent",
            "textPayload":{
               "lang":"en",
               "timestamp":"2020-11-16T06:17:17.943Z",
               "result":{
                  "score":0.686966,
                  "parameters":{
                     "fields":{
                        "value":{
                           "string_value":""
                        },
                        "key":"accountNo"
                     }
                  },
                  "action":"com.tara.bills",
                  "metadata":{
                     "intent_id":"5ff968ab-3646-4abe-ae7e-93203997e5fb",
                     "webhook_used":true,
                     "webhook_for_slot_filling_used":false,
                     "intent_name":"Read Bill",
                     "is_fallback_intent":false
                  },
                  "resolved_query":"Bill details",
                  "action_incomplete":true,
                  "source":"agent",
                  "fulfillment":{
                     "messages":{
                        "speech":{
                           "string_value":"What is your account number?"
                        },
                        "type":{
                           "number_value":0
                        }
                     },
                     "speech":"What is your account number?"
                  },
                  "contexts":{
                     "parameters":{
                        "fields":{
                           "key":"no-match",
                           "value":{
                              "number_value":0
                           }
                        }
                     },
                     "name":"__system_counters__",
                     "lifespan":1
                  }
               },
               "id":"ab128f85-0044-42ba-ae8a-c77d0905d972-aabf4fdf",
               "status":{
                  "error_type":"success",
                  "code":200
               },
               "session_id":"TestTaraSession"
            },
            "severity":"INFO",
            "session_id":"TestTaraSession",
            "resource":{
               "labels":{
                  "project_id":"entuber"
               },
               "type":"global"
            },
            "timestamp":"2020-11-16T06:17:17.965Z",
            "insertId":"dc02v6g3eo2vya",
            "labels":{
               "type":"dialogflow_response",
               "protocol":"V2_BETA1",
               "request_id":"ab128f85-0044-42ba-ae8a-c77d0905d972-aabf4fdf"
            }
         },
         {
            "textPayload":{
               "session_id":"TestTaraSession",
               "status":{
                  "error_type":"success",
                  "code":200
               },
               "result":{
                  "metadata":{
                     "webhook_used":false,
                     "intent_name":"Read Bill",
                     "webhook_response_time":3334,
                     "intent_id":"5ff968ab-3646-4abe-ae7e-93203997e5fb",
                     "is_fallback_intent":false,
                     "webhook_for_slot_filling_used":false
                  },
                  "contexts":{
                     "name":"defaultwelcomeintent-followup",
                     "lifespan":1,
                     "parameters":{
                        "fields":{
                           "value":{
                              "string_value":"Tara"
                           },
                           "key":"name.original"
                        }
                     }
                  },
                  "source":"agent",
                  "action":"com.tara.bills",
                  "fulfillment":{
                     "speech":"Your current bill of $2927.62 is due on 10/19/2020.",
                     "messages":{
                        "speech":{
                           "string_value":"Your current bill of $2927.62 is due on 10/19/2020."
                        },
                        "type":{
                           "number_value":0
                        }
                     }
                  },
                  "score":1,
                  "parameters":{
                     "fields":{
                        "value":{
                           "number_value":21012875
                        },
                        "key":"accountNo"
                     }
                  },
                  "resolved_query":21012875
               },
               "lang":"en",
               "id":"1d3df1d2-1753-48fc-948e-7fc91473d055-aabf4fdf",
               "timestamp":"2020-11-16T06:17:34.184Z"
            },
            "insertId":"1def6i1g15mb48l",
            "labels":{
               "protocol":"V2_BETA1",
               "request_id":"1d3df1d2-1753-48fc-948e-7fc91473d055-aabf4fdf",
               "type":"dialogflow_response"
            },
            "receiveTimestamp":"2020-11-16T06:17:37.548546643Z",
            "timestamp":"2020-11-16T06:17:37.536Z",
            "severity":"INFO",
            "resource":{
               "type":"global",
               "labels":{
                  "project_id":"entuber"
               }
            },
            "session_id":"TestTaraSession",
            "trace":"TestTaraSession",
            "logName":"projects/entuber/logs/dialogflow_agent"
         }
      ]
   },
   {
      "timestamp":"2020-11-16T06:29:14.564Z",
      "session_id":"9b86412b-ec2d-39c0-7cbc-7a44a74a8804",
      "Thread":[
         {
            "timestamp":"2020-11-16T06:29:01.638Z",
            "session_id":"9b86412b-ec2d-39c0-7cbc-7a44a74a8804",
            "logName":"projects/entuber/logs/dialogflow_agent",
            "severity":"INFO",
            "insertId":"1httvn0g2nj3fae",
            "labels":{
               "request_id":"94ec1661-d472-4ef0-a7bd-d85e22062e4d-aabf4fdf",
               "protocol":"V2_BETA1",
               "type":"dialogflow_response",
               "source":"DIALOGFLOW_CONSOLE"
            },
            "trace":"9b86412b-ec2d-39c0-7cbc-7a44a74a8804",
            "resource":{
               "labels":{
                  "project_id":"entuber"
               },
               "type":"global"
            },
            "textPayload":{
               "status":{
                  "error_type":"success",
                  "code":200
               },
               "session_id":"9b86412b-ec2d-39c0-7cbc-7a44a74a8804",
               "result":{
                  "resolved_query":"hello",
                  "contexts":{
                     "parameters":{
                        "fields":{
                           "key":"no-match",
                           "value":{
                              "number_value":0
                           }
                        }
                     },
                     "lifespan":1,
                     "name":"__system_counters__"
                  },
                  "score":1,
                  "action_incomplete":true,
                  "fulfillment":{
                     "messages":{
                        "speech":{
                           "string_value":"What is the name?"
                        },
                        "type":{
                           "number_value":0
                        }
                     },
                     "speech":"What is the name?"
                  },
                  "action":"input.welcome",
                  "metadata":{
                     "is_fallback_intent":false,
                     "intent_id":"d06d9b61-eee2-4481-a038-bc97194d611a",
                     "webhook_for_slot_filling_used":false,
                     "webhook_used":false,
                     "intent_name":"Default Welcome Intent"
                  },
                  "parameters":{
                     "fields":{
                        "key":"name",
                        "value":{
                           "string_value":""
                        }
                     }
                  },
                  "source":"agent"
               },
               "id":"94ec1661-d472-4ef0-a7bd-d85e22062e4d-aabf4fdf",
               "timestamp":"2020-11-16T06:29:01.596Z",
               "lang":"en"
            },
            "receiveTimestamp":"2020-11-16T06:29:01.64859801Z"
         },
         {
            "severity":"INFO",
            "receiveTimestamp":"2020-11-16T06:29:07.676213264Z",
            "resource":{
               "type":"global",
               "labels":{
                  "project_id":"entuber"
               }
            },
            "insertId":"13ojz2yg2npq0nk",
            "labels":{
               "source":"DIALOGFLOW_CONSOLE",
               "type":"dialogflow_response",
               "request_id":"896b6d41-fe8b-4123-ba68-07004435db42-aabf4fdf",
               "protocol":"V2_BETA1"
            },
            "timestamp":"2020-11-16T06:29:07.667Z",
            "logName":"projects/entuber/logs/dialogflow_agent",
            "trace":"9b86412b-ec2d-39c0-7cbc-7a44a74a8804",
            "textPayload":{
               "status":{
                  "error_type":"success",
                  "code":200
               },
               "timestamp":"2020-11-16T06:29:07.62Z",
               "lang":"en",
               "result":{
                  "contexts":{
                     "lifespan":50,
                     "name":"name",
                     "parameters":{
                        "fields":{
                           "key":"name.original",
                           "value":{
                              "string_value":"tara"
                           }
                        }
                     }
                  },
                  "source":"agent",
                  "resolved_query":"tara",
                  "score":1,
                  "action":"input.welcome",
                  "fulfillment":{
                     "messages":{
                        "lang":"en",
                        "speech":{
                           "string_value":""
                        },
                        "type":{
                           "number_value":0
                        }
                     },
                     "speech":"Can I have your name?"
                  },
                  "parameters":{
                     "fields":{
                        "key":"name",
                        "value":{
                           "string_value":"Tara"
                        }
                     }
                  },
                  "metadata":{
                     "webhook_for_slot_filling_used":false,
                     "is_fallback_intent":false,
                     "intent_id":"d06d9b61-eee2-4481-a038-bc97194d611a",
                     "intent_name":"Default Welcome Intent",
                     "webhook_used":false
                  }
               },
               "id":"896b6d41-fe8b-4123-ba68-07004435db42-aabf4fdf",
               "session_id":"9b86412b-ec2d-39c0-7cbc-7a44a74a8804"
            },
            "session_id":"9b86412b-ec2d-39c0-7cbc-7a44a74a8804"
         }
      ]
   }
];

var output = [];

for (let key in input) {
    var result = {
        session_id: input[key].session_id,
        timestamp: input[key].timestamp,
        Thread: []
    };
    for (let th of input[key].Thread) result.Thread.push({
        labels: th.labels,
        speech: th.textPayload.result.fulfillment.speech,
        timestamp: th.textPayload.timestamp,
        id: th.textPayload.id
    });
    output.push(result);
};

console.log(output);


Source: stackoverflow