How to resolve the algorithm JSON step by step in the EGL programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm JSON step by step in the EGL programming language

Table of Contents

Problem Statement

Load a JSON string into a data structure.
Also, create a new data structure and serialize it into JSON. Use objects and arrays (as appropriate for your language) and make sure your JSON is valid (https://jsonformatter.org).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm JSON step by step in the EGL programming language

Source code in the egl programming language

record familyMember
	person person;
	relationships relationship[]?;
end

record person
	firstName string;
	lastName string;
	age int;
end

record relationship
	relationshipType string;
	id int;
end

people Person[]; // Array of people
		
people.appendElement(new Person { firstName = "Frederick", lastName = "Flintstone", age = 35} );
people.appendElement(new Person { firstName = "Wilma", lastName = "Flintstone", age = 34} );
people.appendElement(new Person { firstName = "Pebbles", lastName = "Flintstone", age = 2} );
people.appendElement(new Person { firstName = "Bernard", lastName = "Rubble", age = 32} );
people.appendElement(new Person { firstName = "Elizabeth", lastName = "Rubble", age = 29} );
people.appendElement(new Person { firstName = "Bam Bam", lastName = "Rubble", age = 2} );
	
family Dictionary; // A dictionary of family members using a uid as key
family["1"] = new FamilyMember{ person = people[1], relationships = [new Relationship{ relationshipType="spouse", id = 2 }, new Relationship{ relationshipType="child", id = 3}] };
family["2"] = new FamilyMember{ person = people[2], relationships = [new Relationship{ relationshipType="spouse", id = 1 }, new Relationship{ relationshipType="child", id = 3}] };
family["3"] = new FamilyMember{ person = people[3], relationships = [new Relationship{ relationshipType="mother", id = 2 }, new Relationship{ relationshipType="father", id = 1}] };
family["4"] = new FamilyMember{ person = people[4], relationships = [new Relationship{ relationshipType="spouse", id = 5 }, new Relationship{ relationshipType="child", id = 6}] };
family["5"] = new FamilyMember{ person = people[5], relationships = [new Relationship{ relationshipType="spouse", id = 4 }, new Relationship{ relationshipType="child", id = 6}] };
family["6"] = new FamilyMember{ person = people[6], relationships = [new Relationship{ relationshipType="mother", id = 5 }, new Relationship{ relationshipType="father", id = 4}] };
	
// Convert dictionary of family members to JSON string
jsonString string = jsonLib.convertToJSON(family);

// Show JSON string
SysLib.writeStdout(jsonString);

// Convert JSON string into dictionary of family members
family Dictionary;
jsonLib.convertFromJSON(jsonString, family);				

// List family members and their relationships
familyMember FamilyMember;
relation FamilyMember;
				
keys string[] = family.getKeys();
for(i int from 1 to keys.getSize())
			
	SysLib.writeStdout("----------------------------------------------------");

	familyMember = family[keys[i]];
			
	SysLib.writeStdout(familyMember.person.lastName + ", " + familyMember.person.firstName + " - " + familyMember.person.age);
			
	for(j int from 1 to familyMember.relationships.getSize())
		id string = familyMember.relationships[j].id;
		relation = family[id];
		SysLib.writeStdout(familyMember.relationships[j].relationshipType + ":  " +
		relation.person.lastName + ", " + relation.person.firstName);
	end

end

// Service function definition
function geocode(address String) returns (GoogleGeocoding) {
    @Resource{uri = "binding:GoogleGeocodingBinding"},
    @Rest{method = _GET, uriTemplate = "/json?address={address}&sensor=false",
    requestFormat = None, responseFormat = JSON}
}

// Invoke service function
call geocode("111 Maple Street, Somewhere, CO") returning to callback;

function callBack(result GoogleGeocoding in)
    SysLib.writeStdout(result.status);
    SysLib.writeStdout(result.results[1].geometry.location.lat);
    SysLib.writeStdout(result.results[1].geometry.location.lng);
end

  

You may also check:How to resolve the algorithm Averages/Median step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Motzkin numbers step by step in the REXX programming language
You may also check:How to resolve the algorithm Factorial step by step in the SequenceL programming language
You may also check:How to resolve the algorithm Topic variable step by step in the BASIC256 programming language
You may also check:How to resolve the algorithm Machine code step by step in the D programming language