browsw
asked on
Getting sub element from JSON
I have the following json:
How can I alter this in PHP to just get the following:
Thank you in advance
{
"count":2,
"page":1,
"pageSize":2,
"students":[{"forename":"Joe","surname":"Bloggs"},{"forename":"John","surname":"Doe"}],
"totalCount":2,
"totalPages":1
}
How can I alter this in PHP to just get the following:
{
[{"forename":"Joe","surname":"Bloggs"},{"forename":"John","surname":"Doe"}]
}
Thank you in advance
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?
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.
Thanks
Stuart
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'
)
Thanks
Stuart
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you, Chris. Just what I was after.