How to format a multi line string with triple quotes inside in JSON file? - json

I have markdown string to store in json file, so I am using multi line and so many things in the string. How can I store it in json object?
[
{ "title": "Title of Ebook",
"details": [
{
"head": "Introduction 1",
"data": """It’s important to follow trends in your field to ensure you’re staying current on standards and protocols and perhaps more so in the field of coding. Programmers of all specialties can benefit from following
#h this is a header
__italic__
industry-leading blogs to stay aware of the latest technologies.If you’re a coder of any sort you’ll want to subscribe to these useful programming blogs written by the top blogging coders.Each of these bloggers has made a name for themselves in the programming community by sharing relevant, high-quality information and tips for coders. They maintain their respective blogs well and keep current information posted on a regular basis.By following the best programming blogs you’ll find tips and shortcuts you may never have otherwise thought to try. Consider using an RSS feed reader through your phone or desktop browser to automatically download each new post from these top coding bloggers."""
}
}
]
When I will receive the string I will have the markdown style data from json.

Your object is not closed correctly. There are closing brackets missing.
Not sure if it answers your question but you can do it like this:
import 'dart:convert';
main(List<String> arguments) {
var json = [
{
"title": "Title of Ebook",
"details": [
{
"head": "Introduction 1",
"data":
"""It’s important to follow trends in your field to ensure you’re staying current on standards and protocols and perhaps more so in the field of coding. Programmers of all specialties can benefit from following
#h this is a header
__italic__
industry-leading blogs to stay aware of the latest technologies.If you’re a coder of any sort you’ll want to subscribe to these useful programming blogs written by the top blogging coders.Each of these bloggers has made a name for themselves in the programming community by sharing relevant, high-quality information and tips for coders. They maintain their respective blogs well and keep current information posted on a regular basis.By following the best programming blogs you’ll find tips and shortcuts you may never have otherwise thought to try. Consider using an RSS feed reader through your phone or desktop browser to automatically download each new post from these top coding bloggers."""
}
]
}
];
var encodedJson = jsonEncode(json);
print(encodedJson);
var details = json[0]['details'] as List;
print(details[0]['data']);
}

Related

How to structure a MediaWiki and Cargo query in Visual Basic

I'm trying to pull some specific data from a wiki site; pcgamingwiki.
I would like to pull the save game data path from the site but I'm not understanding how to make the request properly. I also wanna state I'm not very knowledgeable about Visual Basic or even using C#, I did some VB back 5+ years ago and I remember bits a pieces.
What I want my app to do is using the steam game ID to search PCGamingWiki for the relevant game page ID, and then parse the save game location path of that game on that page.
I am using WikiClientLibrary with the Cargo extension support too.
This is the Visual Basic code that I have:
Async Sub main2()
Dim client As New WikiClientLibrary.Client.WikiClient()
Dim site As New WikiClientLibrary.Sites.WikiSite(client, "https://pcgamingwiki.com/w/api.php?")
Await site.Initialization
Dim page = New WikiClientLibrary.Pages.WikiPage(site, 3098)
Await page.RefreshAsync(WikiClientLibrary.Pages.PageQueryOptions.FetchContent)
RichTextBox1.AppendText(site.SiteInfo.SiteName)
RichTextBox1.AppendText(page.Content.Substring(0, 10000))
End Sub
And this Cargo query that gets the page ID from the Steam app ID
https://www.pcgamingwiki.com/w/api.php?action=cargoquery&tables=Infobox_game&fields=Infobox_game._pageID%3DPageID%2CInfobox_game.Steam_AppID&where=Infobox_game.Steam_AppID%20HOLDS%20"21000"&format=jsonfm
This is getting me the page information but I know there is a better way of getting the info.
Doing a wiki query for the sections gives me
{
"toclevel": 2,
"level": "3",
"line": "Save game data location",
"number": "3.2",
"index": "6",
"fromtitle": "Lego_Batman:_The_Videogame",
"byteoffset": 5842,
"anchor": "Save_game_data_location"
}
But the issue is that from what I could find is that I can only use the fromtitle and the index to get this information but on a different game page the fromtitle is different as it is the game name and the index changes as the content can be placed different on the page depending on the game.
I was hoping that I could use "number" as it seems they all are "3.2" or "anchor" as they all have "save_game_data_location" but I can't see how I could search by these identifiers. PCGamingWiki uses the Cargo extension but I'm not sure how I could make the right queries in visual basic to get the information I need or if there is another way of getting this content.
This is what the content on the page looks like
I'm just not sure how I convert the normal API queries cleanly and that Cargo query into Visual Basic code so any advice would be appreciated.

Create REST API without using any backend

How do you create a rest api without using any backend technologies such as node, express? ie: Is it possible to create an api using only client side framework such as react, vue and no server side involved?
I would like to create a very simple rest api which I can host either on github pages or gitlab pages.
I would like a live rest api that I can access via my own domain such as http://username.github.io and not a fake one that you can create using json-server or My JSON Server - and would be able to do the following.
Here is the description of what the api should do.
/quotes.txt - Read the contents of the quotes.txt file and display it
/quotes.json - Read the contents of the quotes.txt file and convert it
to json format
/random.txt - Read the contents of the quotes.txt file and display a
random quote in txt format
/random.json - Read the contents of the quotes.txt file and display a
random quote in json format
Output of the API
/quotes.txt
To be or not to be that is the question - Author Unknown
All your dreams can come true if you have the courage to pursue them - Walt Disney
Stand up for what you believe in even if you are standing alone -
/quotes.json
[
{
"quote": "To be or not to be that is the question",
"author": "Author Unknown"
},
{
"quote": "All your dreams can come true if you have the courage to pursue them",
"author": "Walt Disney"
},
{
"quote": "Stand up for what you believe in even if you are standing alone",
"author": ""
}
]
/random.txt
All your dreams can come true if you have the courage to pursue them - Walt Disney
/random.json
{
"quote": "All your dreams can come true if you have the courage to pursue them",
"author": "Walt Disney"
}
You could use json-server to serve a JSON file. It supports "canonic" ways to express RESTful requests. This tool can be used on a dev machine or on a intranet server. Here's a blog post about how to set it up.
My JSON Server is a service that does the same exact thing in the Internet environment for you. There are also competing services with similar functionalities, e.g. myjson.com. I'm sure you will find more if you search online.
P.S. I'm sure you'll need to do some massaging to actually translate .txt -> .json. I've never seen services that work against .txt files rather than .json ones.
Ugh. I really like question edits that invalidate the answers.

Should the server respond one json for all content data on the page in SPA or is better to split it?

I'm building API for SinglePageApplication, which handle by Angular in frontend. One thing is not clear to me.
Supose the web applcation has delati journal paige wich display journal,some articles which belongs to this journal and some cool authors which can be not connected to this journal.
Should I build my api urls based on each need page content, for example:
from url /api/journal/<journal_id>
send json:
{
"journal": {
"id": 10,
"name": "new_journal",
"articles": [
{
"name": "cool_article",
"id": 42
},
{
"name": "another_cool_article",
"id": 43
}
]
},
"authors": [
{
"name": "some_name",
"id": 42
},
{
"name": "another_name",
"id": 43
}
]
}
Or I should build my api based on concrete objects and related objects of them.
With urls like this:
/api/journals/<journal_id>
/api/authors/
And frontend side build this page with two GET requests for fetching content.
Sory if my question too broad, I just want to find best bractice for building API to SinglePageApplications.
Does it have any difference of building API enpoints for external web-apps and what I should do if page need to display more objects, which not belong together? Which of the options above is better?
There isn't really a universal right answer for this. It largely depends on the use case for that data you're fetching. I would say to err on the side of splitting this into multiple requests as it grants you flexibility and efficiency in terms of partial updates to the page. That approach also makes exposing an API to the public much easier in terms of being able to just expose what you already have.
If you're dealing with a potentially large (an intentionally relative term) number of concurrent requests though, you may build some composites of related data to mitigate that.
Of course, you could also do a combination of the two as well (first load makes 1 large request, subsequent updates are segmented).

Node and Express application sending POST request twice

This is my route code.
router.post('/content', function(req, res) {
var topic = req.body.inputText;
console.log("topic :"+topic); //shows correctly
var somedata = "";
console.log("request arrived for URL", req.url); //prints "request arrived for URL /content" only once
var url = '<the-url-from-which-i-fetch-json'+topic;
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var wikitext = JSON.parse(body);
console.log("Got a response: ", JSON.stringify(wikitext[0]['content'])); //prints the content only once as needed
res.render('newcontent', {
text : JSON.stringify(wikitext[0]['content'])
});
} else {
console.log("Got an error: ", error, ", status code: ", response.statusCode);
}
});
});
This is an instance of json data fetched.
{"topicname":"wiki","content":"<p>A <strong>wiki</strong> ([audio=en-us-wiki.oggˈwɪki] [WIKee]) is a website which allows collaborative modification of its content and structure directly from the web browser. In a typical wiki, text is written using a simplified markup language (known as \"wiki markup\"), and often edited with the help of a rich-text editor.(ref: Britannica)Encyclopædia Britannicavolume=1publisher=Encyclopædia Britannica, Inc.year=2007location=Londonurl=http://www.britannica.com/EBchecked/topic/1192819/wikiaccessdate=April 10, 2008]</ref></p>\n<p>A wiki is run using wiki software, otherwise known as a wiki engine. There are dozens of different wiki engines in use, both standalone and part of other software, such as bug tracking systems. Some wiki engines are open source, whereas others are proprietary. Some permit control over different functions (levels of access); for example, editing rights may permit changing, adding or removing material. Others may permit access without enforcing access control. Other rules may also be imposed to organize content. A wiki engine is a type of content management system, but it differs from most other such systems, including blog software, in that the content is created without any defined owner or leader, and wikis have little implicit structure, allowing structure to emerge according to the needs of the users.(ref: Easy Wiki Hosting )</p>\n<p>The encyclopedia project Wikipedia is by far the most popular wiki-based website, and is in fact one of the most widely viewed sites of any kind of the world, having been ranked in the top ten since 2007. Wikipedia is not a single wiki but rather a collection of hundreds of wikis, one for each language. There are at least tens of thousands of other wikis in use, both public and private, including wikis functioning as knowledge management resources, notetaking tools, community websites and intranets.</p>\n<p>Ward Cunningham, the developer of the first wiki software, WikiWikiWeb, originally described it as \"the simplest online database that could possibly work\". \"Wiki\" (pronounced [ˈwiki][group=note/w/lang=haw] phoneme varies between [wlang=haw] and [vlang=haw], and the realization of the [/k/lang=haw] phoneme varies between [klang=haw] and [tlang=haw], among other realizations. Thus, the pronunciation of the Hawaiian word <em>wiki</em> varies between [wikilang=haw], [witilang=haw], [vikilang=haw], and [vitilang=haw]. See Hawaiian phonology for more details.}}) is a Hawaiian word meaning \"quick\".</p>\n","date":"2016-03-16T03:23:11.735Z"}
This is newcontent.jade
#{text}
p HELLO SSUP
In the output, I get the data of text variable printed twice.I tried on both firefox and chrome, and same issue persists. (There is no favicon loading anywhere, as this was mentioned to be an issue in certain threads). So what can be wrong in this code?
Output has the text printed twice
HELLO SSUP is printed at the end, after both content.
1) You do not need to additionally stringify the wikitext[0]['content'] because its type is already a string:
console.log(typeof wikitext[0]['content'])
// string
2) You use escaping in the template: #{text}
So:
router:
res.render('newcontent', {
text : wikitext[0]['content']
});
template:
!{text}
p HELLO SSUP
or
div!= text
p HELLO SSUP

Does anyone know of a webservice for looking up definitions of words that would be able to return results in JSON?

I found http://words.bighugelabs.com/api.php but nothing like this for definitions/dictionary.
Ideally I'd grab a dictionary file and build my own API for this, but this is for a demo and we need something short-term that can be called from within a javascript function.
wiktionary.org provides an API for example:
http://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=Television&format=json
gives back
{
"query": {"searchinfo": {"totalhits": 208862},
"search": [{
"ns": 0,
"title": "Television",
"snippet": "<span class='searchmatch'>Television<\/span> (TV) is a widely used telecommunication medium for transmitting and receiving moving images , either monochromatic (\"black <b>...<\/b> ",
"size": 28228,
"wordcount": 3566,
"timestamp": "2009-10-02T15:09:56Z"},
...
]},
"query-continue": {"search": {"sroffset":10}}
}
I think this is what you are looking for
bighugelabs API - Json fromat
aonaware services - XML format
Not sure if it would fit your needs, but answers.com has webmaster tools that offer various services, including dictionary lookup. Don't know if any can be called from javascript.
At short notice you could set up a reverse-proxy on your server that lets you AJAX your favorite dictionary website and then 'scrape' the definitions from the document that is returned. It's obviously not a long term solution but for a one time thing, you probably won't get into trouble.
This is a web service and have several dictionaries:
http://services.aonaware.com/DictService/DictService.asmx
P.S. I did not notice the JSON part of your question.