August 5, 2016 at 4:58 pm
Hi,
I have a mix of data in a table as follows:
person_id interests
1             {"team sports": ["soccer", "rugby", "basketball"], "individual sports": ["table tennis", "chess", "dart", "shooting"], "others": "fishing"}
I am trying to achieve the results as follows:
person_id  sport_type      interests
1      team sports            soccer
1      team sports            rugby
1      team sports            basketball
1      individual sports        table tennis
1      individual sports        chess
1      individual sports        dart
1      individual sports        shooting
1      others                  fishing
Can someone help me how to solve this problem as I am new to this OPENJSON query in SQL 2016
Thanks
August 5, 2016 at 5:34 pm
I wrote this query but it is not returning entire results. It is failing when TYPE is not an array e.g. Fishing in this instance as it is not JSON anymore after first split.
Declare @json nvarchar(max)
Set @json = '{
"team sport": ["soccer", "rugby", "basketball"],
"individual sports": ["table tennis", "chess", "dart", "shooting"],
"others": "fishing"
}';
SELECT *
FROM OPENJSON(@json, 'lax $') main
CROSS APPLY OPENJSON(main.value)
August 7, 2016 at 8:18 pm
Any chance of changing the input to make that ["fishing"]?
The error message is that the input is not properly formatted. Making it a one-element array fixes the problem.
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply