- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to correctly get a value from a JSON PHP?
To get a value from JSON, use json_decode(). Let’s say the following is our JSON
$detailsJsonObject = '{"details":[{"name":"John","subjectDetails":{"subjectId":"101","subjectName":"PHP","marks":"58", "teacherName":"Bob"}}]}';
We need to fetch specific values i.e. Subject Name, Marks, etc.
Example
The PHP code is as follows
<!DOCTYPE html> <html> <body> <?php $detailsJsonObject = '{"details":[ {"name":"John","subjectDetails": {"subjectId":"101","subjectName":"PHP","marks":"58", "teacherName":"Bob"} }]}'; $convertToArrayObject = json_decode($detailsJsonObject,true); $actualSubjectName = $convertToArrayObject[details][0][subjectDetails][subjectName]; $actualTeacherName = $convertToArrayObject[details][0][subjectDetails][teacherName]; echo "The Subject Name is=",$actualSubjectName,"<br>"; echo "The Teacher Name is=",$actualTeacherName; ?> </body> </html>
Output
This will produce the following output
The Subject Name is=PHP The Teacher Name is=Bob
- Related Articles
- How to get a JSON field in a nested JSON using Rest Assured?
- How to get file name from a path in PHP?
- How to get parameters from a URL string in PHP?
- Get value for key from nested JSON object in JavaScript
- MongoDB query to find a value from JSON like data?
- How to get a JSON array field in a nested JSON using Rest Assured?
- PHP – How to get the Unicode point value of a given character?
- How to selectively retrieve value from json output JavaScript
- How do I get a value array (instead a json array) greater than 50 in MongoDB?
- How to get a key/value data set from a HTML form?
- How to get a value from the cell of a Pandas DataFrame?
- How to get a value from a nested list in Rest Assured?
- How to get a value for a given key from a Python dictionary?
- How to get the values of the different types from a JSON object in Java?
- How to parse a JSON response and get a particular field from the response in Rest Assured?

Advertisements