Discussions related to Visual Prolog
Rangarajan
Posts: 18
Joined: 1 Jun 2007 14:51

JSON Example

Post by Rangarajan »

I am looking for examples that show how to retrieve the contents of a JSON object. I have seen examples that show how to create and populate a JSON object, but I need to understand how to retrieve the different elements from an existing object.
User avatar
Thomas Linder Puls
VIP Member
Posts: 1471
Joined: 28 Feb 2000 0:01

Re: JSON Example

Post by Thomas Linder Puls »

A JSON object is represented as a map:

Code: Select all

interface jsonObject supports mapM{string, jsonValue}     open core, json ...
You can read about maps (and other interesting stuff) in Collection Library.

You should also notice that a json::jsonValue is a functor domain with several functor alternatives:

Code: Select all

class json     open core   domains     jsonValue =         n;         f;         t;         r(real Number);         s(string String);         a(jsonValue* Array);         o(jsonObject Object)         [presenter(presenter_jsonValue)].     % @short Represent the JSON values corresponding to the ECMA 404 standard.<br>     % n - the JSON constant null<br>     % f - the  JSON constant false<br>     % t - the  JSON constant true<br>     % r(Number) - a JSON number<br>     % s(String) - a JSON string String<br>     % a(Array) - a JSON array as a list<br>     % o(Object) - a JSON object<br>     % See also http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf     % @end ...
And with that in mind ;-) here is a little example that "fetches" stuff in a JSON object:

Code: Select all

implement main     open core   constants     json : string = @' {     "f_int" : 12,     "f_object" :         {             "nested_1" : false,             "nested_2" : [1, 5, { "A": "a" }, 7]         },     "f_string" : "This is a string" }'.   clauses     run() :-         % Parse the json string as a jsonObject         JSON = jsonObject::fromString(json),         stdio::writef("JSON = %p\n", JSON),         % get the "f_object" member         F_Object_value = JSON:get("f_object"),         % unpack the object from the json::jsonValue         json::o(F_Object) == F_Object_value,         stdio::writef("F_Object = %p\n", F_Object),         % get and unpack the "f_object" member         F_Object_2 = JSON:get_object("f_object"),         stdio::writef("F_Object_2 = %p\n", F_Object_2),         % get and unpack the "nested_2" array (from F_Object)         Nested_2 = F_Object:get_array("nested_2"),         stdio::writef("Nested_2 = %p\n", Nested_2).   end implement main
If you need clarification then don't hesitate to ask more.
Regards Thomas Linder Puls
PDC
Rangarajan
Posts: 18
Joined: 1 Jun 2007 14:51

Re: JSON Example

Post by Rangarajan »

Thanks a lot!
Rangarajan
Posts: 18
Joined: 1 Jun 2007 14:51

Re: JSON Example

Post by Rangarajan »

I could get back to this example only now. Thanks again.
Where can I get the complete documentation for reading and parsing JSON objects?
For example, how do I access the nth element of an array inside a JSON object?

Can you include a full independent example of working with JSON objects in your next update?
User avatar
Thomas Linder Puls
VIP Member
Posts: 1471
Joined: 28 Feb 2000 0:01

Re: JSON Example

Post by Thomas Linder Puls »

JSON arrays are mapped into Prolog lists, so treatment of them follows normal list processing rules.

Looping an "N" from 0 to the length of the list and then obtaining the n'th element in the list is a very bad (in-efficient) approach to list handling. But if you insist there is an nth predicate in the list class:

Code: Select all

predicates     nth : (positive Index, Elem* List) -> Elem Item.     % @short Return `Item` of `List` at position `Index`. `Index` is zero based.     % @end
However I suggest you read the Lists and Recursion tutorial first.

JSON is just a "transport" data type, and as such not a major feature of the language. It uses "normal" Visual Prolog "stuff" and should not need independent treatment. So I do not think there will be a more comprehensive example than the one already given here.

There are more relevant tutorials on the tutorials page.
Regards Thomas Linder Puls
PDC
Rangarajan
Posts: 18
Joined: 1 Jun 2007 14:51

Re: JSON Example

Post by Rangarajan »

OK. I will go through the tutorials.

Thanks.