0

I use a default response for a user in my proyect according to the following table.

Table: USERS |ID | NAME | DEFAULTRESPONSE

The default response contains a string in JSON format. I need to change the month and the day from all relatives_birthdays from string to int.

Here is an example of the previous json:

[
 {
  "id":1,
  "relatives_birthdays":[ 
  { 
    "month":"8",
    "day": "1",
    "description": "Birthday mother"
  }, 
  { 
    "month":"3",
    "day": "27",
    "description": "Birthday brother"
  }, 
  { 
    "month":"4",
    "day": "12",
    "description": "Birthday father"
  }
  ]
 },
 {
  "id":2,
  "relatives_birthdays":[ 
  { 
    "month":"12",
    "day": "11",
    "description": "Birthday mother"
  }, 
  { 
    "month":"1",
    "day": "2",
    "description": "Birthday brother"
  }, 
  { 
    "month":"7",
    "day": "18",
    "description": "Birthday father"
  }
  ]
 }
]

And here is the json I need:

[
 {
  "id":1,
  "relatives_birthdays":[ 
  { 
    "month": 8,
    "day": 1,
    "description": "Birthday mother"
  }, 
  { 
    "month": 3,
    "day": 27,
    "description": "Birthday brother"
  }, 
  { 
    "month": 4,
    "day": 12,
    "description": "Birthday father"
  }
  ]
 },
 {
  "id":2,
  "relatives_birthdays":[ 
  { 
    "month": 12,
    "day": 11,
    "description": "Birthday mother"
  }, 
  { 
    "month": 1,
    "day": 2,
    "description": "Birthday brother"
  }, 
  { 
    "month": 7,
    "day": 18,
    "description": "Birthday father"
  }
  ]
 }
]

Any ideas on the script I need to execute in order to accomplish this?

2
  • Does that need to be done with sql-server? or are you open to other alternatives? Commented Nov 15, 2018 at 15:18
  • It must be done with sql-server Commented Nov 15, 2018 at 15:42

1 Answer 1

1

You can parse the json with openjson and then rebuild it. This will works, but it is probably very inefficient:

declare @j nvarchar(max) = '[
 {
  "id":1,
  "relatives_birthdays":[ 
  { 
    "month":"8",
    "day": "1",
    "description": "Birthday mother"
  }, 
  { 
    "month":"3",
    "day": "27",
    "description": "Birthday brother"
  }, 
  { 
    "month":"4",
    "day": "12",
    "description": "Birthday father"
  }
  ]
 },
 {
  "id":2,
  "relatives_birthdays":[ 
  { 
    "month":"12",
    "day": "11",
    "description": "Birthday mother"
  }, 
  { 
    "month":"1",
    "day": "2",
    "description": "Birthday brother"
  }, 
  { 
    "month":"7",
    "day": "18",
    "description": "Birthday father"
  }
  ]
 }
]'

select rt.[id]
 , relatives_birthdays.[month]
 , relatives_birthdays.[day]
 , relatives_birthdays.[description]
from (
 select p.id
 from openjson(@j) with (id int) p
 ) as rt
inner join (
 select p.id
  , c.month
  , c.day
  , c.description
 from openjson(@j) with (
   id int
   , relatives_birthdays nvarchar(max) as json
   ) p
 cross apply openjson(relatives_birthdays) with (
   month int
   , day int
   , description nvarchar(max)
   ) c
 ) as relatives_birthdays
 on rt.id = relatives_birthdays.id
for json auto

This is the resulting json:

[
  {
    "id": 1,
    "relatives_birthdays": [
      {
        "month": 8,
        "day": 1,
        "description": "Birthday mother"
      },
      {
        "month": 3,
        "day": 27,
        "description": "Birthday brother"
      },
      {
        "month": 4,
        "day": 12,
        "description": "Birthday father"
      }
    ]
  },
  {
    "id": 2,
    "relatives_birthdays": [
      {
        "month": 12,
        "day": 11,
        "description": "Birthday mother"
      },
      {
        "month": 1,
        "day": 2,
        "description": "Birthday brother"
      },
      {
        "month": 7,
        "day": 18,
        "description": "Birthday father"
      }
    ]
  }
]
Sign up to request clarification or add additional context in comments.

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.