Parse JSON from Joomla K2 website - json

I'm trying to get JSON from my k2 powered Joomla website. As I understood, I need to add index.php?option=com_k2&id=1&lang=mk&task=category&view=itemlist&format=json to my URL, and then it downloads a joomla.json file. As I know now I need to parse this content but I'm suspicious about the correct format in the joomla.json file.
Here is part of the content of the json file:
{"id":"6","title":"\u0424\u0443\u0441\u0442\u0430\u043d \u041a\u0440\u0438\u0441\u0442\u0438\u043d\u0430","alias":"fustan-kristina","link":"\/katalog\/vencanici\/fustan-kristina.html","catid":"1","introtext":"<ul>\r\n<li>\u041e\u0432\u043e\u0458 \u0444\u0443\u0441\u0442\u0430\u043d \u0435 \u0438\u0437\u0440\u0430\u0431\u043e\u0442\u0435\u043d \u043e\u0434 100% \u0441\u0432\u0438\u043b\u0430.<\/li>\r\n<li>\u041d\u0435\u0436\u0435\u043d \u043c\u0430\u0442\u0435\u0440\u0438\u0458\u0430\u043b<\/li>\r\n<li>\u0414\u043e\u0434\u0430\u0434\u0435\u043d\u0438 \u0446\u0438\u0440\u043a\u043e\u043d\u0438<\/li>\r\n<\/ul>\r\n<p>\u0412\u0438\u0441\u0442\u0438\u043d\u0441\u043a\u0438 \u043d\u0435\u0432\u0435\u0441\u0442\u0438\u043d\u0441\u043a\u0438 \u0444\u0443\u0441\u0442\u0430\u043d<\/p>\r\n<p>\u0411\u0443\u0442\u0438\u043a \u0412\u0438\u043a\u0442\u043e\u0440<\/p>","fulltext":"","extra_fields":[{"id":"1","name":"\u0426\u0435\u043d\u0430","value":"12000 \u0434\u0435\u043d.","type":"textfield","group":"1","published":"1","ordering":"1"},{"id":"6","name":"\u0412\u0435\u0431 \u0421\u0442\u0440\u0430\u043d\u0430","value":"<a href=\"http:\/\/www.domain.mk\" target=\"_blank\">http:\/\/www.domain.mk<\/a>","type":"link","group":"1","published":"1","ordering":"2"}],"created":"2012-07-11 09:42:04","created_by_alias":"","modified":"2012-07-11 11:26:34","featured":"0","image":"\/media\/k2\/items\/cache\/ada9a09acea936d776a6f55c82778c43_S.jpg","imageWidth":"200","image_caption":"\u0424\u0443\u0441\u0442\u0430\u043d \u041a\u0440\u0438\u0441\u0442\u0438\u043d\u0430","image_credits":"","imageXSmall":"\/media\/k2\/items\/cache\/ada9a09acea936d776a6f55c82778c43_XS.jpg","imageSmall":"\/media\/k2\/items\/cache\/ada9a09acea936d776a6f55c82778c43_S.jpg","imageMedium":"\/media\/k2\/items\/cache\/ada9a09acea936d776a6f55c82778c43_M.jpg","imageLarge":"\/media\/k2\/items\/cache\/ada9a09acea936d776a6f55c82778c43_L.jpg","imageXLarge":"\/media\/k2\/items\/cache\/ada9a09acea936d776a6f55c82778c43_XL.jpg","video":null,"video_caption":"","video_credits":"","gallery":null,"hits":"50","category":{"id":"1","name":"\u0412\u0435\u043d\u0447\u0430\u043d\u0438\u0446\u0438","alias":"vencanici","link":"\/katalog\/katalog\/vencanici.html","description":"","image":"","ordering":"1"},"tags":[{"id":"1","name":"kristina","published":"1","link":"\/tag\/kristina.html"},{"id":"2","name":"fustan","published":"1","link":"\/tag\/fustan.html"},{"id":"3","name":"vencanica","published":"1","link":"\/tag\/vencanica.html"}],"attachments":[],"votingPercentage":100,"numOfvotes":"(1 \u0413\u043b\u0430\u0441\u0430\u0458)","author":{"name":"\u0414\u0430\u0440\u043a\u043e \u041f\u0435\u0442\u043a\u043e\u0432\u0441\u043a\u0438","link":"\/blog\/\u0414\u0430\u0440\u043a\u043e-\u041f\u0435\u0442\u043a\u043e\u0432\u0441\u043a\u0438.html","avatar":"http:\/\/www.gravatar.com\/avatar\/bb9f8918a0a63b260d46eb419bf1a894?s=100&default=http%3A%2F%domain.mk%2Fcomponents%2Fcom_k2%2Fimages%2Fplaceholder%2Fuser.png","profile":{"gender":null}},"numOfComments":"0","events":{"BeforeDisplay":"","AfterDisplay":"","AfterDisplayTitle":"","BeforeDisplayContent":"","AfterDisplayContent":"","K2BeforeDisplay":"","K2AfterDisplay":"","K2AfterDisplayTitle":"","K2BeforeDisplayContent":"","K2AfterDisplayContent":"","K2CommentsCounter":""}}]}
Is this json in the correct format,and is this the method that I'm doing correct to get json content from my K2 website?

You are having an extra ] and } at the end of your JSON String.
After removing them, your JSON turns out fine and legit.
{"id":"6","title":"\u0424\u0443\u0441\u0442\u0430\u043d \u041a\u0440\u0438\u0441\u0442\u0438\u043d\u0430","alias":"fustan-kristina","link":"\/katalog\/vencanici\/fustan-kristina.html","catid":"1","introtext":"<ul>\r\n<li>\u041e\u0432\u043e\u0458 \u0444\u0443\u0441\u0442\u0430\u043d \u0435 \u0438\u0437\u0440\u0430\u0431\u043e\u0442\u0435\u043d \u043e\u0434 100% \u0441\u0432\u0438\u043b\u0430.<\/li>\r\n<li>\u041d\u0435\u0436\u0435\u043d \u043c\u0430\u0442\u0435\u0440\u0438\u0458\u0430\u043b<\/li>\r\n<li>\u0414\u043e\u0434\u0430\u0434\u0435\u043d\u0438 \u0446\u0438\u0440\u043a\u043e\u043d\u0438<\/li>\r\n<\/ul>\r\n<p>\u0412\u0438\u0441\u0442\u0438\u043d\u0441\u043a\u0438 \u043d\u0435\u0432\u0435\u0441\u0442\u0438\u043d\u0441\u043a\u0438 \u0444\u0443\u0441\u0442\u0430\u043d<\/p>\r\n<p>\u0411\u0443\u0442\u0438\u043a \u0412\u0438\u043a\u0442\u043e\u0440<\/p>","fulltext":"","extra_fields":[{"id":"1","name":"\u0426\u0435\u043d\u0430","value":"12000 \u0434\u0435\u043d.","type":"textfield","group":"1","published":"1","ordering":"1"},{"id":"6","name":"\u0412\u0435\u0431 \u0421\u0442\u0440\u0430\u043d\u0430","value":"<a href=\"http:\/\/www.domain.mk\" target=\"_blank\">http:\/\/www.domain.mk<\/a>","type":"link","group":"1","published":"1","ordering":"2"}],"created":"2012-07-11 09:42:04","created_by_alias":"","modified":"2012-07-11 11:26:34","featured":"0","image":"\/media\/k2\/items\/cache\/ada9a09acea936d776a6f55c82778c43_S.jpg","imageWidth":"200","image_caption":"\u0424\u0443\u0441\u0442\u0430\u043d \u041a\u0440\u0438\u0441\u0442\u0438\u043d\u0430","image_credits":"","imageXSmall":"\/media\/k2\/items\/cache\/ada9a09acea936d776a6f55c82778c43_XS.jpg","imageSmall":"\/media\/k2\/items\/cache\/ada9a09acea936d776a6f55c82778c43_S.jpg","imageMedium":"\/media\/k2\/items\/cache\/ada9a09acea936d776a6f55c82778c43_M.jpg","imageLarge":"\/media\/k2\/items\/cache\/ada9a09acea936d776a6f55c82778c43_L.jpg","imageXLarge":"\/media\/k2\/items\/cache\/ada9a09acea936d776a6f55c82778c43_XL.jpg","video":null,"video_caption":"","video_credits":"","gallery":null,"hits":"50","category":{"id":"1","name":"\u0412\u0435\u043d\u0447\u0430\u043d\u0438\u0446\u0438","alias":"vencanici","link":"\/katalog\/katalog\/vencanici.html","description":"","image":"","ordering":"1"},"tags":[{"id":"1","name":"kristina","published":"1","link":"\/tag\/kristina.html"},{"id":"2","name":"fustan","published":"1","link":"\/tag\/fustan.html"},{"id":"3","name":"vencanica","published":"1","link":"\/tag\/vencanica.html"}],"attachments":[],"votingPercentage":100,"numOfvotes":"(1 \u0413\u043b\u0430\u0441\u0430\u0458)","author":{"name":"\u0414\u0430\u0440\u043a\u043e \u041f\u0435\u0442\u043a\u043e\u0432\u0441\u043a\u0438","link":"\/blog\/\u0414\u0430\u0440\u043a\u043e-\u041f\u0435\u0442\u043a\u043e\u0432\u0441\u043a\u0438.html","avatar":"http:\/\/www.gravatar.com\/avatar\/bb9f8918a0a63b260d46eb419bf1a894?s=100&default=http%3A%2F%domain.mk%2Fcomponents%2Fcom_k2%2Fimages%2Fplaceholder%2Fuser.png","profile":{"gender":null}},"numOfComments":"0","events":{"BeforeDisplay":"","AfterDisplay":"","AfterDisplayTitle":"","BeforeDisplayContent":"","AfterDisplayContent":"","K2BeforeDisplay":"","K2AfterDisplay":"","K2AfterDisplayTitle":"","K2BeforeDisplayContent":"","K2AfterDisplayContent":"","K2CommentsCounter":""}}
Use this site to test your JSON String.
http://jsonlint.com/
If you are having difficulty visualizing your JSON String in 3D, try http://jsonviewer.stack.hu/ You could format the String to visualize the JSON.

I am using &format=json too to bulid my website.
$.get('//url with format=json',function(d){$('.k2ItemTitle').html(d.item.title)})
I don't even have to use $.pareJSON

Related

unable to put json file in an ordered HTML that looks prettier

I am trying to put my contents of JSON files in a prettier HTML format with tables and rows and probably colors too but have no idea how can I do that. Can anyone please assist. Below are two JSON files I am trying to put it in HTML file
'''
{"CorrelationId": "awsnightlyendtoend_zone08_20190828T2319", "ValidationType": "validate_adwactivityfact", "Success": false, "OutputPath": ["s3://zone08-data-validation/awsnightlyendtoend_zone08_20190828T2319/validate_adwactivityfact/requires-replay.csv", "s3://zone08-data-validation/awsnightlyendtoend_zone08_20190828T2319/validate_adwactivityfact/require-investigation.csv"], "ValidationDetail": "Comparison Failed: matched=146455, missing=6, mismatched=0, percent success=99.9918066925666%"}
{"CorrelationId": "awsnightlyendtoend_zone50_20190828T2303", "ValidationType": "validate_adwactivityfact", "Success": false, "OutputPath": ["s3://zone50-data-validation/awsnightlyendtoend_zone50_20190828T2303/validate_adwactivityfact/requires-replay.csv", "s3://zone50-data-validation/awsnightlyendtoend_zone50_20190828T2303/validate_adwactivityfact/require-investigation.csv"], "ValidationDetail": "Comparison Failed: matched=145541, missing=24, mismatched=0, percent success=99.96702504036%"}
'''
If you're only going to do it once, you can use an online json to html converter (like this one: http://convertjson.com/json-to-html-table.htm
If not, you'll have to use some javascript to convert parse the json and display as html.
(Probably just jQuery may do the trick)

Transform json using jslt to include space character

How to transform a JSON payload to include a space character before and after colon(:)
The Source Json needs to be transformed to Target Json.
how to do this in JSLT, Please suggest other options as well. Thanks
Source Json
{
"code":"ABC",
"description":"XYZ"
}
Target Json
{
"code" : "ABC",
"description" : "XYZ"
}
If you mean the Java JSLT this isn't something it does. It works on the JSON objects, so from JSLT's point of view the input and the output are the same.
You can probably do what you want to do by loading the JSON, then controlling how it's serialized. This will actually give you spaces on each side of the colon:
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(input));
If you need more detailed control you can implement your own PrettyPrinter to set this up however you want.

Make fields inside a JSON response clickable (url) on a JSP page

I wish to display the JSON response returned from a REST API call "as-is" on a JSP. The problem I'm facing here is that the response is in a single line like below.
{"result":[{"enable":"true","time_stamp":"2018-01-26 19:31:37","url":"abc.xyz.com","group_id":"one"},{"enable":"false","time_stamp":"2018-01-26 19:31:37","url":"lmn.pqr.com","group_id":"two"}]}
I would like it to be displayed in a well-formatted JSON response, something like below for sorts.
{
"result":[
{
"enable":"true",
"time_stamp":"2018-01-26 19:31:37",
"url":"abc.xyz.com",
"group_id":"one"
},
{
"enable":"false",
"time_stamp":"2018-01-26 19:31:37",
"url":"lmn.pqr.com",
"group_id":"two"
}
]
}
Having said that, there are certain url ("url":"abc.xyz.com")parameters in the JSON response, which I would want to be clickable, so that users can see the JSON response dumped on the web page, but be able to click on the URL field's value to navigate to the different page.
Any idea of how this could be achieved?
Just use JSON.stringify and a HTML tag of pre
Here is a fiddle demo
or if your use AngularJs use:
<pre>{{jsonData|json}}</pre>

Display JSON object nicely with Syntax Hihjlighter

I'm trying to display a JSON object nicely (this means on several lines with indentation) with Alex Gorbatchev plugin : http://alexgorbatchev.com/SyntaxHighlighter/
Unfortunately, it all displays on a single line.
I'm using the javascript brush.
I've created a code pen : http://codepen.io/hugsbrugs/pen/XJVjjP?editors=101
var json_object = {"hello":{"my_friend":"gérard", "my_dog":"billy"}};
$('#nice-json').html('<pre class="brush: javascript">' + JSON.stringify(json_object) + '</pre>');
SyntaxHighlighter.highlight();
Please don't give a list of other plugins since I know there is a bunch but I don't want to load additional plugins ... I'd like to achieve it with this plugin.
Thanks for your help
Try indenting the json with the stringify method.
JSON.stringify(json_object, undefined, 2);
You can use the optional third parameter of JSON.stringify(...) which is the space argument.
Change:
JSON.stringify(json_object)
to:
JSON.stringify(json_object, null, '\t')
Here is your codepen updated to show the result of the above modifications. The above modification causes your JSON to be pretty printed over multiple lines.

reading cookie value in play framewrok 2.2 scala template

I am trying to read a cookie value inside the play framework template (not inside a controller). I am trying the following which is not working:
# val cookieVal = request.cookies.get('PLAY_SESSION').value
Any suggestions to fix this will be greatly appreciated. The reason why I am trying this is to change how the page gets rendered based on a cookie value.
In templates you define vals as follows:
#defining(request.cookies.get('PLAY_SESSION').value) { theValue =>
<div>Hello #theValue</div>
}
I personally prefer to read the cookies in the controller and pass them to the template if needed.
suppose PLAY_SESSION stored "37f0983881ba00636868b42234a360d466fb944c-block_status=0&userId=159313257462171"
and you have to render on the basis of the value of block_status.
then in this case you can get its value by
session.get("block_status").get
to use it in template you have to import#implicit session:play.api.mvc.Session at your template.
now you can easily get values at template by #session.get("block_status").get
request.cookies.get("Org").get.value