JSON: Difference between revisions

Content added Content deleted
No edit summary
No edit summary
Line 213: Line 213:
}</lang>
}</lang>
<pre>{"foo":1,"bar":[10,"apples"]}</pre>
<pre>{"foo":1,"bar":[10,"apples"]}</pre>

=={{header|Factor}}==
<lang Factor>
USING: json.writer json.reader ;

SYMBOL: foo

! Load a JSON string into a data structure
"[[\"foo\",1],[\"bar\",[10,\"apples\"]]]" json> foo set


! Create a new data structure and serialize into JSON
{ { "blue" { "ocean" "water" } } >json
</lang>

=={{header|Fantom}}==

<lang fantom>
using util

class Json
{
public static Void main ()
{
Str input := """{"blue": [1, 2], "ocean": "water"}"""
Map jsonObj := JsonInStream(input.in).readJson

echo ("Value for 'blue' is: " + jsonObj["blue"])
jsonObj["ocean"] = ["water":["cold", "blue"]]
Map ocean := jsonObj["ocean"]
echo ("Value for 'ocean/water' is: " + ocean["water"])
output := JsonOutStream(Env.cur.out)
output.writeJson(jsonObj)
echo ()
}
}
</lang>

Output:
<pre>
Value for 'blue' is: [1, 2]
Value for 'ocean/water' is: [cold, blue]
{"blue":[1,2],
"ocean":{"water":["cold","blue"]}}
</pre>

=={{header|EGL}}==
=={{header|EGL}}==


Structures used both to construct and to parse JSON:
Structures used both to construct and to parse JSON strings:
<lang EGL>
<lang EGL>record familyMember
record familyMember
person person;
person person;
relationships relationship[]?;
relationships relationship[]?;
Line 277: Line 230:
relationshipType string;
relationshipType string;
id int;
id int;
end
end</lang>
</lang>


Construct JSON string:
Construct JSON string:
<lang EGL>
<lang EGL>people Person[]; // Array of people
people Person[]; // Array of people
people.appendElement(new Person { firstName = "Frederick", lastName = "Flintstone", age = 35} );
people.appendElement(new Person { firstName = "Frederick", lastName = "Flintstone", age = 35} );
Line 291: Line 242:
people.appendElement(new Person { firstName = "Bam Bam", lastName = "Rubble", age = 2} );
people.appendElement(new Person { firstName = "Bam Bam", lastName = "Rubble", age = 2} );
family Dictionary; // Construct dictionary using uid as key
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["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["2"] = new FamilyMember{ person = people[2], relationships = [new Relationship{ relationshipType="spouse", id = 1 }, new Relationship{ relationshipType="child", id = 3}] };
Line 299: Line 250:
family["6"] = new FamilyMember{ person = people[6], relationships = [new Relationship{ relationshipType="mother", id = 5 }, new Relationship{ relationshipType="father", id = 4}] };
family["6"] = new FamilyMember{ person = people[6], relationships = [new Relationship{ relationshipType="mother", id = 5 }, new Relationship{ relationshipType="father", id = 4}] };
// Convert family members to JSON
// Convert dictionary of family members to JSON string
jsonString string = jsonLib.convertToJSON(family);
jsonString string = jsonLib.convertToJSON(family);


// Show JSON string
// Show JSON string
SysLib.writeStdout(jsonString);
SysLib.writeStdout(jsonString);</lang>
</lang>


Raw Output:
Raw Output:
<pre>
<pre>{
{
"1":{"person":{"firstName":"Frederick","lastName":"Flintstone","age":35},"relationships":[{"relationshipType":"spouse","id":2},{"relationshipType":"child","id":3}]},
"1":{"person":{"firstName":"Frederick","lastName":"Flintstone","age":35},"relationships":[{"relationshipType":"spouse","id":2},{"relationshipType":"child","id":3}]},
"2":{"person":{"firstName":"Wilma","lastName":"Flintstone","age":34},"relationships":[{"relationshipType":"spouse","id":1},{"relationshipType":"child","id":3}]},
"2":{"person":{"firstName":"Wilma","lastName":"Flintstone","age":34},"relationships":[{"relationshipType":"spouse","id":1},{"relationshipType":"child","id":3}]},
Line 315: Line 264:
"5":{"person":{"firstName":"Elizabeth","lastName":"Rubble","age":29},"relationships":[{"relationshipType":"spouse","id":4},{"relationshipType":"child","id":6}]},
"5":{"person":{"firstName":"Elizabeth","lastName":"Rubble","age":29},"relationships":[{"relationshipType":"spouse","id":4},{"relationshipType":"child","id":6}]},
"6":{"person":{"firstName":"Bam Bam","lastName":"Rubble","age":2},"relationships":[{"relationshipType":"mother","id":5},{"relationshipType":"father","id":4}]}
"6":{"person":{"firstName":"Bam Bam","lastName":"Rubble","age":2},"relationships":[{"relationshipType":"mother","id":5},{"relationshipType":"father","id":4}]}
}</pre>
}
</pre>


Validated Output (partial):
Validated Output (partial):
<pre>
<pre>{
{
"1": {
"1": {
"person": {
"person": {
Line 342: Line 289:


Parse JSON:
Parse JSON:
<lang EGL>// Convert JSON string into dictionary of family members
<lang EGL>
// Convert JSON string into dictionary of family members
family Dictionary;
family Dictionary;
jsonLib.convertFromJSON(jsonString, family);
jsonLib.convertFromJSON(jsonString, family);
Line 356: Line 302:
SysLib.writeStdout("----------------------------------------------------");
SysLib.writeStdout("----------------------------------------------------");


SysLib.writeStdout(keys[i]);
familyMember = family[keys[i]];
familyMember = family[keys[i]];
Line 365: Line 310:
relation = family[id];
relation = family[id];
SysLib.writeStdout(familyMember.relationships[j].relationshipType + ": " +
SysLib.writeStdout(familyMember.relationships[j].relationshipType + ": " +
relation.person.lastName + ", " + relation.person.firstName + " - " + relation.person.age);
relation.person.lastName + ", " + relation.person.firstName);
end
end


end
end</lang>

Output:
<pre>Flintstone, Frederick - 35
spouse: Flintstone, Wilma
child: Flintstone, Pebbles
----------------------------------------------------
Flintstone, Wilma - 34
spouse: Flintstone, Frederick
child: Flintstone, Pebbles
----------------------------------------------------
Flintstone, Pebbles - 2
mother: Flintstone, Wilma
father: Flintstone, Frederick
----------------------------------------------------
Rubble, Bernard - 32
spouse: Rubble, Elizabeth
child: Rubble, Bam Bam
----------------------------------------------------
Rubble, Elizabeth - 29
spouse: Rubble, Bernard
child: Rubble, Bam Bam
----------------------------------------------------
Rubble, Bam Bam - 2
mother: Rubble, Elizabeth
father: Rubble, Bernard</pre>

=={{header|Factor}}==
<lang Factor>
USING: json.writer json.reader ;

SYMBOL: foo

! Load a JSON string into a data structure
"[[\"foo\",1],[\"bar\",[10,\"apples\"]]]" json> foo set


! Create a new data structure and serialize into JSON
{ { "blue" { "ocean" "water" } } >json
</lang>
</lang>

=={{header|Fantom}}==

<lang fantom>
using util

class Json
{
public static Void main ()
{
Str input := """{"blue": [1, 2], "ocean": "water"}"""
Map jsonObj := JsonInStream(input.in).readJson

echo ("Value for 'blue' is: " + jsonObj["blue"])
jsonObj["ocean"] = ["water":["cold", "blue"]]
Map ocean := jsonObj["ocean"]
echo ("Value for 'ocean/water' is: " + ocean["water"])
output := JsonOutStream(Env.cur.out)
output.writeJson(jsonObj)
echo ()
}
}
</lang>

Output:
<pre>
Value for 'blue' is: [1, 2]
Value for 'ocean/water' is: [cold, blue]
{"blue":[1,2],
"ocean":{"water":["cold","blue"]}}
</pre>


=={{header|Go}}==
=={{header|Go}}==