How to resolve the algorithm JSON step by step in the Prolog programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm JSON step by step in the Prolog 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 Prolog programming language
Source code in the prolog programming language
:- use_module([ library(http/json),
library(func) ]).
test_json('{"widget": { "debug": "on", "window": { "title": "Sample Konfabulator Widget", "name": "main_window", "width": 500, "height": 500 }, "image": { "src": "Images/Sun.png", "name": "sun1", "hOffset": 250, "vOffset": 250, "alignment": "center" }, "text": { "data": "Click Here", "size": 36, "style": "bold", "name": "text1", "hOffset": 250, "vOffset": 100, "alignment": "center", "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;" }}}').
reading_JSON_term :-
atom_json_dict(test_json(~), Dict, []), %% This accomplishes reading in the JSON data
writeln( 'JSON as Prolog dict: ~w~n'
$ Dict),
writeln( 'Access field "widget.text.data": ~s~n'
$ Dict.widget.text.data),
writeln( 'Alter field "widget": ~w~n'
$ Dict.put(widget, "Altered")).
searalize_a_JSON_term :-
Dict = _{book:_{title:"To Mock a Mocking Bird",
author:_{first_name:"Ramond",
last_name:"Smullyan"},
publisher:"Alfred A. Knopf",
year:1985
}},
json_write(current_output, Dict). %% This accomplishes serializing the JSON object.
?- reading_JSON_term.
JSON as Prolog dict: _G5217{widget:_G5207{debug:on,image:_G5123{alignment:center,hOffset:250,name:sun1,src:Images/Sun.png,vOffset:250},text:_G5189{alignment:center,data:Click Here,hOffset:250,name:text1,onMouseUp:sun1.opacity = (sun1.opacity / 100) * 90;,size:36,style:bold,vOffset:100},window:_G5077{height:500,name:main_window,title:Sample Konfabulator Widget,width:500}}}
Access field "widget.text.data": Click Here
Alter field "widget": _G5217{widget:Altered}
true.
?- searalize_a_JSON_term.
{
"book": {
"author": {"first_name":"Ramond", "last_name":"Smullyan"},
"publisher":"Alfred A. Knopf",
"title":"To Mock a Mocking Bird",
"year":1985
}
}
true.
You may also check:How to resolve the algorithm Solve the no connection puzzle step by step in the Python programming language
You may also check:How to resolve the algorithm Multiple distinct objects step by step in the Pascal programming language
You may also check:How to resolve the algorithm Reverse words in a string step by step in the Gema programming language
You may also check:How to resolve the algorithm Modular exponentiation step by step in the Factor programming language
You may also check:How to resolve the algorithm String prepend step by step in the F# programming language