0

I have to write a query to retrieve data in single json object format.

I have mixed columns. few of them are JSON and few of them are numerical values. While retrieving data i should convert each row to single JSON object. My query is giving me single object but it is giving me results with \\ slashes. Can someone help me in reformatting the below query which should exclude the slashes while formatting each row into single JSON object.

(select 
(select 
p.personReferenceNumber as personReferenceNumber,
personIdentity as personIdentity,
isnull([name],'')  as [name],
p.gender as gender,
isnull(p.birthDateHijri,'') as birthDateHijri,
isnull(p.birthDateGregorian,'') as birthDateGregorian,
isnull(p.liveStatus,'') as liveStatus,
isnull(p.nationality,'') as nationality,
isnull(p.specific,'') as specific,
isnull(p.isDeleted,'') as isDeleted,
isnull(p.sessionId,'') as sessionId,
isnull(p.insertedBy,'') as insertedBy,
isnull(p.insertedTimeStamp,'') as insertedTimeStamp,
isnull(p.updatedBy,'') as updatedBy,
isnull(p.updatedTimestamp,'') as updatedTimestamp
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER) AS person
from person p)

Output:

{\"messageId\":\"f616dbd3-1352-404b-939e-5b12f90b57fe\",
 \"transactionRowId\":594834948322275328,
 \"personId\":\"bebox13\",
 \"idType\":2,\"issueDateHijri\":14400203,
 \"issueDateGregorian\":\"2019-04-12T03:00:00\",
 \"expiryDateHijri\":14400104,
 \"expiryDateGregorian\":\"2019-04-12T03:00:00\",
 \"issuePlace\":[\"Update\",
 \"Update\"]}",
"name":
"{\\\"messageId\\\":\\\"f616dbd3-1352-404b-939e-5b12f90b57fe\\\",
  \\\"transactionRowId\\\":594834948322275328,
  \\\"firstName\\\":[\\\"Update\\\",\\\"Update\\\"],\\\"secondName\\\":[\\\"Update\\\",\\\"Update\\\"],\\\"thirdName\\\":[\\\"Update\\\",\\\"Update\\\"],\\\"familyName\\\":[\\\"Update\\\",\\\"Update\\\"]}"
3
  • 1
    Possible duplicate of JSON without array wrapper on lower levels Commented Jul 8, 2019 at 9:16
  • The fields in your output don't match those in your query. Also your outer SELECT is returning the result of your FOR JSON. Commented Jul 8, 2019 at 9:24
  • @PeterSmith: JSON_QUERY(columName) solves my problem. Thanks. Commented Jul 8, 2019 at 9:49

0

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.