

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- MongoDB query to find a value from JSON like data?
- How to get a JSON field in a nested JSON using Rest Assured?
- Get value for key from nested JSON object in JavaScript
- How to get file name from a path in PHP?
- How to get parameters from a URL string in PHP?
- How to get a JSON array field in a nested JSON using Rest Assured?
- How to get a value for a given key from a Python dictionary?
- How to selectively retrieve value from json output - JavaScript
- How to selectively retrieve value from json output JavaScript
- 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 more than a particular value from varchar column in MySQL?
- How to return a json object from a Python function?
- How to construct a JSON object from a subset of another JSON object in Java?
Advertisements