У меня есть вопрос о том, как настроить формат json с использованием SQL Таблица сервера empjson
:
CREATE TABLE [dbo].[empjson]
(
[productid] [varchar](50) NULL,
[productamount] [varchar](50) NULL,
[Peffectivedate] [date] NULL,
[eventdate] [varchar](50) NULL,
[acharge] [varchar](50) NULL,
[gstamount] [varchar](50) NULL,
[cgstamount] [varchar](50) NULL,
[sgstamount] [varchar](50) NULL,
[closingvalue] [nchar](10) NULL,
[pinvestid] [varchar](50) NULL,
[closingvalue1] [varchar](50) NULL,
[ipinvestid1] [varchar](50) NULL,
[closingvalue2] [varchar](50) NULL,
[investid2] [varchar](50) NULL,
[closingvlaue2] [varchar](50) NULL,
[investid3] [varchar](50) NULL
)
INSERT INTO [dbo].[empjson] ([productid], [productamount], [Peffectivedate], [eventdate], [acharge], [gstamount], [cgstamount], [sgstamount], [closingvalue], [pinvestid], [closingvalue1], [ipinvestid1], [closingvalue2], [investid2], [closingvlaue2], [investid3])
VALUES (N'1', N'20000', CAST(N'2019-02-02' AS Date), N'05-04-2019', N'2400', N'90', N'45', N'45', N'3760 ', N'type1', N'7520', N'type2', N'11280', N'type3', N'15040', N'type2')
Исходя из приведенных выше данных, я хочу вывод в формате JSON как это:
{"amount":{"amount":20000,"currency":"INR"},"instructionType":"ALLOCATION","sourceEvent":"E_PREMIUM_RECEIVED","productid":"{{1}}","effectiveDate":"{{2019-02-02}}","eventDate":"{{2019-05-04}}","subInstructionType":"PREMIUM","details":[{"instructionType":"ALLOCATION","subInstructionType":"CHARGE","amount":{"amount":2400,"currency":"INR"},"details":[{"instructionType":"ALLOCATION","subInstructionType":"CHARGE","amount":{"amount":2400,"currency":"INR"},"dimensions":{"chargeName":"Allocation Charges","chargeCode":"COV_BAS_ULIP1","subChargeCode":"ALLO_CHG"},"details":[{"instructionType":"ALLOCATION","subInstructionType":"TAX","amount":{"amount":90,"currency":"INR"},"details":[{"instructionType":"ALLOCATION","subInstructionType":"TAX","amount":{"amount":45,"currency":"INR"},"dimensions":{"taxType":"CGST"}},{"instructionType":"ALLOCATION","subInstructionType":"TAX","amount":{"amount":45,"currency":"INR"},"dimensions":{"taxType":"SGST","stateCode":"MH"}}]}]}]},{"amount":{"amount":3760,"currency":"INR"},"instructionType":"ALLOCATION","subInstructionType":"investid","details":[{"amount":{"amount":3760,"currency":"INR"},"dimensions":{"investidCode":investid1}},{"amount":{"amount":7520,"currency":"INR"},"dimensions":{"investidCode":investid2}}]}]}:
Я пытался это SQL выражение:
SELECT
productamount AS [amount.amount],
'INR' AS [amount.currency],
'ALLOCATION' AS instructionType,
'E_PREMIUM_RECEIVED' AS sourceEvent,
productid AS [policynumber],
peffectivedate ,
EventDate,
'PREMIUM' AS [subInstructionType],
'ALLOCATION' AS [details.instructionType],
'CHARGE' AS [details.subInstructionType],
Acharge AS [details.amount.amount],
'INR' AS [details.amount.currency],
'Allocation Charges' AS [details.dimensions.chargeName],
'COV_BAS_ULIP1' AS [details.dimensions.chargeCode],
'ALLO_CHG' as [details.dimensions.subChargeCode]
FROM
empjson
FOR JSON PATH
Этот запрос не возвращает ожидаемый JSON. Не могли бы вы помочь мне, как решить эту задачу на SQL Сервер?