Converting JSON file data values to SQL tables rows and columns












0















I have the below code in JSON file :



{
"took": 196,
"timed_out": false,
"_shards": {
"total": 15,
"successful": 15,
"failed": 0
},
"hits": {
"total": 165,
"max_score": null,
"hits": [
{
"_index": "logstash-2018.11.22",
"_type": "nagios_core",
"_id": "AWc6C_EtHRYvW4hmI7sl",
"_score": null,
"_source": {
"message": "EXTERNAL COMMAND: ACKNOWLEDGE_SVC_PROBLEM;DE-Hoeheinoed-VOC1-SRV;ntp_timesync;2;0;0;Jaizel Jem Perdon;SN 307185410",
"@version": "1",
"@timestamp": "2018-11-22T06: 12: 00.307Z",
"host": "172.26.66.59",
"port": 44154,
"type": "nagios_core",
"epoch_timestamp": "1542867118",
"nagios_severity_label": "EXTERNAL COMMAND",
"nagios_external_command": "ACKNOWLEDGE_SVC_PROBLEM",
"nagios_host": "DE-Hoeheinoed-VOC1-SRV",
"nagios_service": "ntp_timesync",
"nagios_sticky": "2",
"nagios_notify": "0",
"nagios_persistent": "0",
"nagios_author": "Jaizel Jem Perdon",
"nagios_comment": "SN 307185410",
"utc_timestamp": "2018-11-22T06: 11: 58.000Z"
},
"sort": [
1542867120307
]
}

]
}
}


And i have the below code in SQL: However, I am getting null values in my result. As am new to JSON, am not able to find out the path of the JSON data values



Drop table if exists  #Temp1
Declare @JSON nvarchar(max)
SELECT @JSON = BulkColumn
FROM OPENROWSET (BULK '\DKRDSDFSROOT10Data_TempMEIPEITE1452552_02test.json', SINGLE_CLOB) as j


select @json as details


If (ISJSON(@json) = 1)
BEGIN
PRINT 'JOSN File is valid';

select * into #Temp1
from OPENJSON(@JSON, '$.hits')
WITH
(
[nagios_author] nvarchar(100) '$.hits.hits._source.nagios_author',
[nagios_comment] nvarchar(100) '$.hits.hits._source.nagios_comment'
)

END
ELse

Begin
PRINT 'JOSN File is invalid';

END


select * from #Temp1


Can someone please help me ?










share|improve this question





























    0















    I have the below code in JSON file :



    {
    "took": 196,
    "timed_out": false,
    "_shards": {
    "total": 15,
    "successful": 15,
    "failed": 0
    },
    "hits": {
    "total": 165,
    "max_score": null,
    "hits": [
    {
    "_index": "logstash-2018.11.22",
    "_type": "nagios_core",
    "_id": "AWc6C_EtHRYvW4hmI7sl",
    "_score": null,
    "_source": {
    "message": "EXTERNAL COMMAND: ACKNOWLEDGE_SVC_PROBLEM;DE-Hoeheinoed-VOC1-SRV;ntp_timesync;2;0;0;Jaizel Jem Perdon;SN 307185410",
    "@version": "1",
    "@timestamp": "2018-11-22T06: 12: 00.307Z",
    "host": "172.26.66.59",
    "port": 44154,
    "type": "nagios_core",
    "epoch_timestamp": "1542867118",
    "nagios_severity_label": "EXTERNAL COMMAND",
    "nagios_external_command": "ACKNOWLEDGE_SVC_PROBLEM",
    "nagios_host": "DE-Hoeheinoed-VOC1-SRV",
    "nagios_service": "ntp_timesync",
    "nagios_sticky": "2",
    "nagios_notify": "0",
    "nagios_persistent": "0",
    "nagios_author": "Jaizel Jem Perdon",
    "nagios_comment": "SN 307185410",
    "utc_timestamp": "2018-11-22T06: 11: 58.000Z"
    },
    "sort": [
    1542867120307
    ]
    }

    ]
    }
    }


    And i have the below code in SQL: However, I am getting null values in my result. As am new to JSON, am not able to find out the path of the JSON data values



    Drop table if exists  #Temp1
    Declare @JSON nvarchar(max)
    SELECT @JSON = BulkColumn
    FROM OPENROWSET (BULK '\DKRDSDFSROOT10Data_TempMEIPEITE1452552_02test.json', SINGLE_CLOB) as j


    select @json as details


    If (ISJSON(@json) = 1)
    BEGIN
    PRINT 'JOSN File is valid';

    select * into #Temp1
    from OPENJSON(@JSON, '$.hits')
    WITH
    (
    [nagios_author] nvarchar(100) '$.hits.hits._source.nagios_author',
    [nagios_comment] nvarchar(100) '$.hits.hits._source.nagios_comment'
    )

    END
    ELse

    Begin
    PRINT 'JOSN File is invalid';

    END


    select * from #Temp1


    Can someone please help me ?










    share|improve this question



























      0












      0








      0








      I have the below code in JSON file :



      {
      "took": 196,
      "timed_out": false,
      "_shards": {
      "total": 15,
      "successful": 15,
      "failed": 0
      },
      "hits": {
      "total": 165,
      "max_score": null,
      "hits": [
      {
      "_index": "logstash-2018.11.22",
      "_type": "nagios_core",
      "_id": "AWc6C_EtHRYvW4hmI7sl",
      "_score": null,
      "_source": {
      "message": "EXTERNAL COMMAND: ACKNOWLEDGE_SVC_PROBLEM;DE-Hoeheinoed-VOC1-SRV;ntp_timesync;2;0;0;Jaizel Jem Perdon;SN 307185410",
      "@version": "1",
      "@timestamp": "2018-11-22T06: 12: 00.307Z",
      "host": "172.26.66.59",
      "port": 44154,
      "type": "nagios_core",
      "epoch_timestamp": "1542867118",
      "nagios_severity_label": "EXTERNAL COMMAND",
      "nagios_external_command": "ACKNOWLEDGE_SVC_PROBLEM",
      "nagios_host": "DE-Hoeheinoed-VOC1-SRV",
      "nagios_service": "ntp_timesync",
      "nagios_sticky": "2",
      "nagios_notify": "0",
      "nagios_persistent": "0",
      "nagios_author": "Jaizel Jem Perdon",
      "nagios_comment": "SN 307185410",
      "utc_timestamp": "2018-11-22T06: 11: 58.000Z"
      },
      "sort": [
      1542867120307
      ]
      }

      ]
      }
      }


      And i have the below code in SQL: However, I am getting null values in my result. As am new to JSON, am not able to find out the path of the JSON data values



      Drop table if exists  #Temp1
      Declare @JSON nvarchar(max)
      SELECT @JSON = BulkColumn
      FROM OPENROWSET (BULK '\DKRDSDFSROOT10Data_TempMEIPEITE1452552_02test.json', SINGLE_CLOB) as j


      select @json as details


      If (ISJSON(@json) = 1)
      BEGIN
      PRINT 'JOSN File is valid';

      select * into #Temp1
      from OPENJSON(@JSON, '$.hits')
      WITH
      (
      [nagios_author] nvarchar(100) '$.hits.hits._source.nagios_author',
      [nagios_comment] nvarchar(100) '$.hits.hits._source.nagios_comment'
      )

      END
      ELse

      Begin
      PRINT 'JOSN File is invalid';

      END


      select * from #Temp1


      Can someone please help me ?










      share|improve this question
















      I have the below code in JSON file :



      {
      "took": 196,
      "timed_out": false,
      "_shards": {
      "total": 15,
      "successful": 15,
      "failed": 0
      },
      "hits": {
      "total": 165,
      "max_score": null,
      "hits": [
      {
      "_index": "logstash-2018.11.22",
      "_type": "nagios_core",
      "_id": "AWc6C_EtHRYvW4hmI7sl",
      "_score": null,
      "_source": {
      "message": "EXTERNAL COMMAND: ACKNOWLEDGE_SVC_PROBLEM;DE-Hoeheinoed-VOC1-SRV;ntp_timesync;2;0;0;Jaizel Jem Perdon;SN 307185410",
      "@version": "1",
      "@timestamp": "2018-11-22T06: 12: 00.307Z",
      "host": "172.26.66.59",
      "port": 44154,
      "type": "nagios_core",
      "epoch_timestamp": "1542867118",
      "nagios_severity_label": "EXTERNAL COMMAND",
      "nagios_external_command": "ACKNOWLEDGE_SVC_PROBLEM",
      "nagios_host": "DE-Hoeheinoed-VOC1-SRV",
      "nagios_service": "ntp_timesync",
      "nagios_sticky": "2",
      "nagios_notify": "0",
      "nagios_persistent": "0",
      "nagios_author": "Jaizel Jem Perdon",
      "nagios_comment": "SN 307185410",
      "utc_timestamp": "2018-11-22T06: 11: 58.000Z"
      },
      "sort": [
      1542867120307
      ]
      }

      ]
      }
      }


      And i have the below code in SQL: However, I am getting null values in my result. As am new to JSON, am not able to find out the path of the JSON data values



      Drop table if exists  #Temp1
      Declare @JSON nvarchar(max)
      SELECT @JSON = BulkColumn
      FROM OPENROWSET (BULK '\DKRDSDFSROOT10Data_TempMEIPEITE1452552_02test.json', SINGLE_CLOB) as j


      select @json as details


      If (ISJSON(@json) = 1)
      BEGIN
      PRINT 'JOSN File is valid';

      select * into #Temp1
      from OPENJSON(@JSON, '$.hits')
      WITH
      (
      [nagios_author] nvarchar(100) '$.hits.hits._source.nagios_author',
      [nagios_comment] nvarchar(100) '$.hits.hits._source.nagios_comment'
      )

      END
      ELse

      Begin
      PRINT 'JOSN File is invalid';

      END


      select * from #Temp1


      Can someone please help me ?







      sql json






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 12:09









      MatBailie

      59.7k1477111




      59.7k1477111










      asked Nov 22 '18 at 12:03









      MariaMaria

      13




      13
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You are using the second hits array in your json as an object, but its an array try to change your query as follows:



           select * into #Temp1
          from OPENJSON(@JSON, '$.hits')
          WITH
          (
          [nagios_author] nvarchar(100) '$.hits.hits[0]._source.nagios_author',
          [nagios_comment] nvarchar(100) '$.hits.hits[0]._source.nagios_comment'
          )





          share|improve this answer































            0














            I tried using below code and it worked



            WITH
            (
            [nagios_author] nvarchar(100) '$.hits[0]._source.nagios_author',
            [nagios_comment] nvarchar(100) '$.hits[0]._source.nagios_comment'

            )






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430615%2fconverting-json-file-data-values-to-sql-tables-rows-and-columns%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              You are using the second hits array in your json as an object, but its an array try to change your query as follows:



               select * into #Temp1
              from OPENJSON(@JSON, '$.hits')
              WITH
              (
              [nagios_author] nvarchar(100) '$.hits.hits[0]._source.nagios_author',
              [nagios_comment] nvarchar(100) '$.hits.hits[0]._source.nagios_comment'
              )





              share|improve this answer




























                0














                You are using the second hits array in your json as an object, but its an array try to change your query as follows:



                 select * into #Temp1
                from OPENJSON(@JSON, '$.hits')
                WITH
                (
                [nagios_author] nvarchar(100) '$.hits.hits[0]._source.nagios_author',
                [nagios_comment] nvarchar(100) '$.hits.hits[0]._source.nagios_comment'
                )





                share|improve this answer


























                  0












                  0








                  0







                  You are using the second hits array in your json as an object, but its an array try to change your query as follows:



                   select * into #Temp1
                  from OPENJSON(@JSON, '$.hits')
                  WITH
                  (
                  [nagios_author] nvarchar(100) '$.hits.hits[0]._source.nagios_author',
                  [nagios_comment] nvarchar(100) '$.hits.hits[0]._source.nagios_comment'
                  )





                  share|improve this answer













                  You are using the second hits array in your json as an object, but its an array try to change your query as follows:



                   select * into #Temp1
                  from OPENJSON(@JSON, '$.hits')
                  WITH
                  (
                  [nagios_author] nvarchar(100) '$.hits.hits[0]._source.nagios_author',
                  [nagios_comment] nvarchar(100) '$.hits.hits[0]._source.nagios_comment'
                  )






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 13:44









                  Voice Of The RainVoice Of The Rain

                  123211




                  123211

























                      0














                      I tried using below code and it worked



                      WITH
                      (
                      [nagios_author] nvarchar(100) '$.hits[0]._source.nagios_author',
                      [nagios_comment] nvarchar(100) '$.hits[0]._source.nagios_comment'

                      )






                      share|improve this answer




























                        0














                        I tried using below code and it worked



                        WITH
                        (
                        [nagios_author] nvarchar(100) '$.hits[0]._source.nagios_author',
                        [nagios_comment] nvarchar(100) '$.hits[0]._source.nagios_comment'

                        )






                        share|improve this answer


























                          0












                          0








                          0







                          I tried using below code and it worked



                          WITH
                          (
                          [nagios_author] nvarchar(100) '$.hits[0]._source.nagios_author',
                          [nagios_comment] nvarchar(100) '$.hits[0]._source.nagios_comment'

                          )






                          share|improve this answer













                          I tried using below code and it worked



                          WITH
                          (
                          [nagios_author] nvarchar(100) '$.hits[0]._source.nagios_author',
                          [nagios_comment] nvarchar(100) '$.hits[0]._source.nagios_comment'

                          )







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 23 '18 at 10:29









                          MariaMaria

                          13




                          13






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430615%2fconverting-json-file-data-values-to-sql-tables-rows-and-columns%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                              Alcedinidae

                              Origin of the phrase “under your belt”?