0

Is there any way to insert data from Json which data stored in several rows, insert into a regular table at first I try to use FOR JSON AUTO but it returns NULL

SELECT *
FROM OPENJSON((SELECT * FROM JsonOneColumn))

Json table is like this

| Value                                                           | 
| --------------------------------------------------------------- | 
| {"Fname":"Cake","Fcount":3,"FDate":"2020-02-13","Fregion":"UK"} | 
| {"Fname":"Coca","Fcount":5,"FDate":"2020-02-13","Fregion":"US"} | 
...

it should be like this:

| Fname | Fcount | FDate    | Fregion |  
| ----- | ------ | -------- | ------- |
| Cake  | 3      |2020-02-13| UK      |
| Coca  | 5      |2020-02-13| US      |
3
  • Is JsonOneColumn the name of the table where the json data are being stored? Commented Jan 10, 2021 at 7:28
  • Yes, that s the name of the table that Json stored in my DB Commented Jan 10, 2021 at 7:29
  • Is JSON stored as VARCHAR? Commented Jan 10, 2021 at 8:04

3 Answers 3

1

Assuming your JSON data is stored in a way similar to the following:

CREATE TABLE Logs (
    _id bigint primary key identity,
    log nvarchar(max)
);

ALTER TABLE Logs
    ADD CONSTRAINT [Log record should be formatted as JSON]
                   CHECK (ISJSON(log)=1)
                   
INSERT INTO Logs
VALUES
('{"Fname":"Cake","Fcount":3,"FDate":"2020-02-13","Fregion":"UK"}'),
('{"Fname":"Coca","Fcount":5,"FDate":"2020-02-13","Fregion":"US"}') 

you can query the data like this:

SELECT 
  JSON_VALUE(log, '$.Fname') AS Fname
  , CAST(JSON_VALUE(log, '$.Fcount') AS INT) AS Fcount
  , CAST(JSON_VALUE(log, '$.FDate') AS DATETIME) AS FDate
  , JSON_VALUE(log, '$.Fregion') AS Fregion
FROM Logs

Output:

Fname Fcount FDate Fregion
Cake 3 2020-02-13T00:00:00Z UK
Coca 5 2020-02-13T00:00:00Z US

Demo here

Sign up to request clarification or add additional context in comments.

Comments

0

I think it will be my answer:

CREATE VIEW OneColumnIntoView
AS  
SELECT *
FROM OPENJSON((SELECT CONCAT('[',(STRING_AGG(VALUE,',')),']') AS JC FROM OneColumnJsonFood))

WITH (Fname NVARCHAR(50),
Fcount INT,
Datetype NVARCHAR(4000),
Fregion NVARCHAR(50)) AS WithJson

1 Comment

Hi @sara moradi : this post isn't an answer, you should add this answer in your question for your tried. I voted your question for give good feedback but you should read how to answer questions in help center. (delete this answer)
0

Try this one :

you need use PK table and here this name is @id you can choose another. i don't know about your data types


---"Fname":"Cake","Fcount":3,"FDate":"2020-02-13","Fregion":"UK"

SET NOCOUNT ON;  
  
DECLARE @id INT
Declare @table table (Fname nvarchar(50), Fcount int,FDate date,Fregion varchar(10))  
  
DECLARE CR CURSOR FOR   
SELECT [Value] FROM JsonOneColumn 
  
OPEN CR  
  
FETCH NEXT FROM CR   
INTO @id
  
WHILE @@FETCH_STATUS = 0  
BEGIN  

    Declare @test nvarchar(500) = (SELECT [Value] FROM JsonOneColumn where id = @id)
    
    Insert into @table (Fname, Fcount,FDate,Fregion)
    Select Fname, Fcount,FDate,Fregion
    From OpenJSON (@test) With ( Fname   nvarchar(50)   '$.Fname'
                                ,Fcount  int            '$.Fcount'
                                ,FDate   date           '$.FDate'
                                ,Fregion varchar(10))   '$.Fregion')
  
 
        -- Get the next id.  
    FETCH NEXT FROM CR   
    INTO @id  
END   
CLOSE CR;  
DEALLOCATE CR;  

-- Just for check out put
Select * From @table

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.