Link to home
Start Free TrialLog in
Avatar of browsw
browsw

asked on

Getting sub element from JSON

I have the following json:
{
"count":2,
"page":1,
"pageSize":2,
"students":[{"forename":"Joe","surname":"Bloggs"},{"forename":"John","surname":"Doe"}],
"totalCount":2,
"totalPages":1
}

Open in new window


How can I alter this in PHP to just get the following:
{
[{"forename":"Joe","surname":"Bloggs"},{"forename":"John","surname":"Doe"}]
}

Open in new window


Thank you in advance
Avatar of Jim Riddles
Jim Riddles
Flag of United States of America image

I'm not positive what you are asking here.  Are you saying that you have a PHP page that generates that JSON, and you only want to generate the students node?  Or are you saying that your PHP receives this JSON and you want to access the students node?
Avatar of browsw
browsw

ASKER

Hi Jim,
Sorry was a bit vague. I'm getting the top response from an API call to a web app. I need to use the response in an SQL query using OPENJSON so just need the students element.

declare @JSON nvarchar(max) = '[{"forename":"Joe","surname":"Bloggs"},{"forename":"John","surname":"Doe"}]';
SELECT * FROM OPENJSON(@json)
 WITH 
 (
   surname NVARCHAR(MAX) '$.surname', forename NVARCHAR(MAX) '$.forename'
 )

Open in new window


Thanks
Stuart
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of browsw

ASKER

Thank you, Chris. Just what I was after.