JSON Functions¶
Functions of the json group
json\encode( )¶
The json\encode() function transforms the provided VALUE into a JSON-formatted string and returns it.
Example
json\encode() Example
// Create an object
$object = object\create();
object\set($object, 'firstName', 'Albert');
object\set($object, 'lastName', 'Einstein');
// Encode the object to a JSON string
$json = json\encode($object);
json\retrieve( )¶
The json\retrieve() function retrieves a specific attribute from a JSON string. PATH is a string, items are separated by dots.
Examples:
Retrieving id from {"id": "SOME_ID"}:
Retrieving id from [{"id": "SOME_ID"}]:
Demonstrates the creation of a more complex object structure, including its corresponding JSON representation.
json\retrieve() Example 3
// Define student 1 - Albert Einstein
$student1 = object\create();
object\set($student1, 'id', '0001');
object\set($student1, 'firstName', 'Albert');
object\set($student1, 'lastName', 'Einstein');
// Define student 1 results
$student1Results = object\create();
object\set($student1Results, 'Science', 'A');
object\set($student1Results, 'Maths', 'A+');
object\set($student1Results, 'English', 'B');
object\set($student1Results, 'Physics', 'A+');
// Add results to student 1 object
object\set($student1, 'results', $student1Results);
// Define student 2 - Issac Newton
$student2 = object\create();
object\set($student2, 'id', '0002');
object\set($student2, 'firstName', 'Issac');
object\set($student2, 'lastName', 'Newton');
// Define student 2 results
$student2Results = object\create();
object\set($student2Results, 'Science', 'A+');
object\set($student2Results, 'Maths', 'A+');
object\set($student2Results, 'English', 'C');
object\set($student2Results, 'Chemistry', 'C');
// Add results to student 2 object
object\set($student2, 'results', $student2Results);
// Define student 3 - Max Plank
$student3 = object\create();
object\set($student3, 'id', '0003');
object\set($student3, 'firstName', 'Max');
object\set($student3, 'lastName', 'Plank');
// Define student 3 results
$student3Results = object\create();
object\set($student3Results, 'Science', 'A');
object\set($student3Results, 'Maths', 'B');
object\set($student3Results, 'English', 'C');
// Add results to student 3 object
object\set($student3, 'results', $student3Results);
// Define student 4 - Charles Darwin
$student4 = object\create();
object\set($student4, 'id', '0004');
object\set($student4, 'firstName', 'Charles');
object\set($student4, 'lastName', 'Darwin');
// Define student 4 results
$student4Results = object\create();
object\set($student4Results, 'Science', 'B');
object\set($student4Results, 'Maths', 'C');
object\set($student4Results, 'English', 'B');
// Add results to student 4 object
object\set($student4, 'results', $student4Results);
// Define student 5 - Marie Curie
$student5 = object\create();
object\set($student5, 'id', '0005');
object\set($student5, 'firstName', 'Marie');
object\set($student5, 'lastName', 'Curie');
// Define student 5 results
$student5Results = object\create();
object\set($student5Results, 'Science', 'B');
object\set($student5Results, 'Maths', 'C');
object\set($student5Results, 'English', 'B');
// Add results to student 5 object
object\set($student5, 'results', $student5Results);
// Define student 6 - Nikola Tesla
$student6 = object\create();
object\set($student6, 'id', '0006');
object\set($student6, 'firstName', 'Nikola');
object\set($student6, 'lastName', 'Tesla');
// Define student 6 results
$student6Results = object\create();
object\set($student6Results, 'Science', 'B');
object\set($student6Results, 'Maths', 'B+');
object\set($student6Results, 'English', 'C');
object\set($student6Results, 'Physics', 'A+');
// Add results to student 6 object
object\set($student6, 'results', $student6Results);
// Create an array of student objects
$listOfStudents = array\push($listOfStudents, $student1, $student2, $student3, $student4, $student5, $student6);
// Create a students object and add the array of students to the object
$students = object\create();
object\set($students, 'students', $listOfStudents);
// Encode the students object to a JSON string
$json = json\encode($students);
$firstName = json\retrieve($json, 'students.4.firstName');
$lastName = json\retrieve($json, 'students.4.lastName');
$scienceResult = json\retrieve($json, 'students.4.results.Science');
output\printLine( string\concatenate('First Name: ', $firstName) ); // Prints Marie
output\printLine( string\concatenate('Last Name: ', $lastName) ); // Prints Curie
output\printLine( string\concatenate('Science Result: ', $scienceResult) ); // Prints B
Example 4 JSON Structure
{
"students": [
{
"id": "0001",
"firstName": "Albert",
"lastName": "Einstein",
"results": {
"Science": "A",
"Maths": "A+",
"English": "B",
"Physics": "A+"
}
},
{
"id": "0002",
"firstName": "Issac",
"lastName": "Newton",
"results": {
"Science": "A+",
"Maths": "A+",
"English": "C",
"Chemistry": "C"
}
},
{
"id": "0003",
"firstName": "Max",
"lastName": "Plank",
"results": {
"Science": "A",
"Maths": "B",
"English": "C"
}
},
{
"id": "0004",
"firstName": "Charles",
"lastName": "Darwin",
"results": {
"Science": "B",
"Maths": "C",
"English": "B"
}
},
{
"id": "0005",
"firstName": "Marie",
"lastName": "Curie",
"results": {
"Science": "B",
"Maths": "C",
"English": "B"
}
},
{
"id": "0006",
"firstName": "Nikola",
"lastName": "Tesla",
"results": {
"Science": "B",
"Maths": "B+",
"English": "C",
"Physics": "A+"
}
}
]
}
See Also¶
- Array Functions
- Datetime Functions
- Entity Functions
- Env Functions
- Ext Functions
- JSON Functions
- Language Functions
- Number Functions
- Object Functions
- Output Functions
- Password Functions
- Record Functions
- String Functions
- Mythradon Marketing
- Mythradon Sales
- Mythradon Service
- Mythradon System Administration
- Mythradon Tools