json goes RichMemo lazarus / 21-General ---------------------------------------------------------------------- Nicole · 2025-07-03 14:28 Repeatadly I use json-strings in my code. It is hard to read. Parsing to a string, - is even harder. I failed so many times and lost information and read error-messages... Is there a tool, which extracts json nicely to a string to use it e.g. in Listboxes or RichMemos? what I dream of: - shall find out by itself which information is available - shall be robust and quiet if the information is damaged (I cannot click away dozens of error messages) Here comes an example of what I would like to parse to a RichMemo (one of a great amount of formats) Code: Diff  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {  "status" : "ok",  "totalResults" : 3279,  "articles" : [    {      "source" : {        "id" : "cbs-news",        "name" : "CBS News"      },      "author" : "Kerry  Breen",      "title" : "Bryan Kohberger expected to plead guilty in murders of Idaho students",      "description" : "Bryan Kohberger is expected to plead guilty in the 2022 slayings of four University of Idaho students in a deal that will spare him from the death penalty.",      "url" : "https://www.cbsnews.com/news/bryan- kohberger-university-of-idaho-student-murders-plea-hearing/",      "urlToImage" : "https://assets3.cbsnewsstatic.com/hub/i/r/2025/03/07/2 f72c02f-2278-4f5f-b0a3- c9a5807156cb/thumbnail/1200x630/41dc633d26b13e8bdd42172f001325dc/kohbe rger.jpg",      "publishedAt" : "2025-07-02T13:02:44Z",      "content" : "Bryan Kohberger, the former criminal justice doctoral student charged in the murders of four University of Idaho students in 2022, is expected to officially plead guilty Wednesday in a deal with pros… [+4378 chars]"    },    {      "source" : {        "id" : "the-times- of-india",        "name" : "The Times of India"      },      "author" : "Global Desk",      "title" : "Elon Musk loses $12,000,000,000 in one day amid Trump feud and Tesla crash — here’s what caused second- biggest wealth wipeout in human history",      "description" : "Elon Musk just lost a jaw-dropping $12,000,000,000 in one day, and the world is watching. The loss came after a heated Trump feud and a dramatic Tesla stock crash, shaking investor confidence. As Trump threatened to pull EV tax credits and SpaceX contracts, M…",      "url" : "https://economictimes.indiatimes.com/news/international/us/elon-musk- loses-12000000000-in-one-day-amid-trump-feud-and-tesla-crash-heres- what-caused-second-biggest-wealth-wipeout-in-human- history/articleshow/122206909.cms",      "urlToImage" : "https://img.e timg.com/thumb/msid-122207336,width-1200,height-630,imgsize- 118178,overlay-economictimes/articleshow.jpg",      "publishedAt" : "2025-07-02T12:52:51Z",      "content" : "Elon Musk suffers $12 billion loss in just one day amid Trump feud, Tesla stock plunge: On July 12, 2025, the worlds richest man, Elon Musk, lost a staggering $12 billion in a single day. The massive… [+6188 chars]"    },    {    "source" : {        "id" : null,        "name" : "Idnes.cz"      },      "author" : "iDNES.cz and 1000 lines more ---------------------------------------------------------------------- CM630 · 2025-07-03 14:30 Before you go further in that direction - TRichMemo seems abandoned. ---------------------------------------------------------------------- rvk · 2025-07-03 14:42 Quote from: CM630 on July 03, 2025, 04:30:20 pmBefore you go further in that direction - TRichMemo seems abandoned. Or it's already perfected  That set aside... This question seems more to be about a function to do prettyprint on json. Otherwise @Nicole, what is your desired output?? ---------------------------------------------------------------------- paweld · 2025-07-03 15:07 @Nicole: Try json viewer, eg. https://github.com/gcarreno/laz-JSON- Viewer ---------------------------------------------------------------------- Nicole · 2025-07-03 15:32 What my output would be the best for me? This hard to say. e.g. in the case of the above news function I dream of keying in "Tesla" and see in return: 12. 11. 2024 Tesla car burnt in New York (click here for more)  1.   3.  2025 new factory built in France (Reuters, click here to see full article)  3.   7.  2025 Musk produces a Trump Killer Car (White House, click here for a dic pic of Trump) and so on. The best thing ever would be, if there would be news filters in languages I do not speak. But this is fantasy. I am not sure, if there shall be a clickable link or not. This may cause more troubles than it solves. Better it is NOT clickable. If RichMemo is abandoned: what do you suggest? Once I tried this htmlView and - returned to the snapshot without it. I never said, that CEF would be a sauries because we must not say this here. So let me put it like this: too large for me, I tried it. TMemo is even less flexible. TListView is hard to fill. TListbox makes it hard to work with the complete text. Thank you for the link with the Laz-Json Viewer. How can I use it? Is it a component or a tool? (at the moment I work on a Win 7 VM) ---------------------------------------------------------------------- Thaddy · 2025-07-03 15:46 Quote from: Nicole on July 03, 2025, 05:32:35 pmIf RichMemo is abandoned: what do you suggest? It is not abandoned. It just does not need updates.... People like to look at the last release date and that is a wrong way of judging software quality so: Ignore that answer, it does not expose any real knowledge. After all, have a look at the fpc 3.2.2 release date... Nuff said. RvK wrote about the same btw. The full sourcecode is available, so when something needs fixing it will be fixed. There are no open issues regarding TRichmemo that I am aware of.... ---------------------------------------------------------------------- paweld · 2025-07-03 16:56 Quote from: Nicole on July 03, 2025, 05:32:35 pmThank you for the link with the Laz-Json Viewer. How can I use it? Is it a component or a tool? (at the moment I work on a Win 7 VM) This is tool. 1. download source from: https://github.com/gcarreno/laz-JSON- Viewer/archive/refs/heads/master.zip 2. Unzip 3. Open in Lazarus ( src / lazJSONViewer.lpi ) 4. Build It doesn't matter if your OS is Windows or Linux ---------------------------------------------------------------------- rvk · 2025-07-03 17:10 Quote from: Nicole on July 03, 2025, 05:32:35 pmWhat my output would be the best for me? This hard to say. e.g. in the case of the above news function I dream of keying in "Tesla" and see in return: 12. 11. 2024 Tesla car burnt in New York (click here for more)  1.   3.  2025 new factory built in France (Reuters, click here to see full article)  3.   7.  2025 Musk produces a Trump Killer Car (White House, click here for a dic pic of Trump) and so on. The best thing ever would be, if there would be news filters in languages I do not speak. But this is fantasy. That looks like you want to take something like from https://newsapi.org/ and create your own cliënt (like groundnews) to search for news  There is no ready made component for you to use. You will have to create the retrieval, parsing, searching and displaying of the result yourself. I don't think the component you use to view this is very important. You can start by just using trichmemo and putting the result in there. You can also convert your results to html and use one of the htmlviewers. The most important part is creating a standalone component which retrieves and filters to your desired result. Keep the visual and functional parts separated. So first create a TMyNews component which retrieves the data and where you can set a filter after which it spits out your results. Then you can take that result and display it like you want (converting it for RTF of HTML. BTW. I searched for newsapi and fpc the Google AI already gave me a complete example to retrieve the newsapi.org and filter it's result to a TMemo (without me even asking for it) ---------------------------------------------------------------------- creaothceann · 2025-07-03 17:19 I'd first recreate the structure in code: Code: [Select]unit UForm_NewsArticles; interface uses // ... type TSource = class ID, Name : string; end; TArticle = class Source                                                            : TSource; Author, Title, Description, URL, URL_to_Image, PublishedAt, Content : string; end; TArticles = class constructor Create(const SourceText : string); destructor  Destroy;                            override; private var      _Articles     : array of TArticle; var      _Status       : boolean; function _TotalResults : integer; public property Status       : boolean read _Status; property TotalResults : integer read _TotalResults; end; // TForm_NewsArticles definition here implementation uses FpJson, JsonParser;  // https://wiki.freepascal.org/fcl-json // ... end. Then write the Create constructor to call the JSON parser, while creating the structure in memory. Then display a list of articles in a TListView (with ViewStyle = vsReport and OwnerData = True). The currently selected article can be displayed below in a TMemo or something else. ---------------------------------------------------------------------- d2010 · 2025-07-03 17:47 Quote from: paweld link=topic=71658.msg559663#msg559663 This is tool. 1. download source from: [urlhttps://github.com/gcarreno/laz-JSON- Viewer/archive/refs/heads/master.zip[/url] 2. Unzip 3. Open in Lazarus ( src / lazJSONViewer.lpi ) 4. Build It doesn't matter if your OS is Windows or Linux For one file , D:disk, in my computer work good , but for multiple files *.json, same to me, is worst. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$R *.res}Var json1:string='';    boca:integer=00;begin  RequireDerivedFormResource:=True;  Application.Title:='laz-JSON- Viewer';  Application.Scaled:=True;  Application.Initialize;  Application.CreateForm(TfrmMain, frmMain);  Json1:=paramstr(01);  boca:=0000;  if (length(json1)>03) then boca:=pos('*',json1)+001;  if (boca=01) and (FileExists(json1)) then frmMain.LoadFile(json1) else  if (boca>01)  ???  Application.Run;end. Do not forget, you move from "private ...procedure LoadFile(const AFilename: UTF8String);  " to " public  procedure LoadFile(const AFilename: UTF8String);     " C:Q1= I do not understand, UTF8String is more-equal with Shortstring? Why he do not use String ? ---------------------------------------------------------------------- Nicole · 2025-07-03 18:03 Thank you all for those ideas! @rvk those units are work in progress since several days. There is one unit for yahoo, one for Alpha Vantage, Figi, ttingo, marketwatch,... My "one frame does it all" became monstroes before. However you seem to have golden fingers: I searched for newsapi and fpc the Google AI already gave me a complete example to retrieve the newsapi.org and filter it's result to a TMemo (without me even asking for it)  I spent a lot of time there, however it did not bless me with this. Can you kindly post a copy or let me have the link? ---------------------------------------------------------------------- cdbc · 2025-07-03 18:04 Hi @d2010: Well, UTF8String is an 'AnsiString' with its codepage set to CP_UTF8, thus it's allocated on the heap and is managed -- a pointer- type -- Otoh, a 'Shortstring' S is a value-type, i.e.: an array[0..255] of (ansi)char; Where the S[0] is a char whose ordinal value corresponds to the length of the string (1..255) and the actual string-data start @ S[1]. It's the oldest stringtype in pascal and also the one to use in record- fields. You can also use it across library borders (dll/so). Regards Benny ---------------------------------------------------------------------- rvk · 2025-07-03 18:16 Quote from: Nicole on July 03, 2025, 08:03:14 pmI searched for newsapi and fpc the Google AI already gave me a complete example to retrieve the newsapi.org and filter it's result to a TMemo (without me even asking for it)  I spent a lot of time there, however it did not bless me with this. Can you kindly post a copy or let me have the link? It was just from a search (see image). The code it gave me was conceptual so probably doesn't run directly. But you probably already have similar code. Besides, you can also use chatgpt, copilot or perplexity AI's to give you more accurate code. Example (conceptual): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  System.SysUtils,  IdHTTP,  IdBaseComponent,  IdComponent,  IdTCPConnection,  IdTCPClient,  IdIOHandler,  IdSSLOpenSSL,  IdSSLContext,  IdMessageClient,  fcl.json; var  HTTP: TIdHTTP;  Response: string;  JSON: TJSONObject;  Articles: TJSONArray;  i: Integer;  Article: TJSONObject;  Title: string;  URL: string;begin  HTTP := TIdHTTP.Create(nil);  try    // Replace with your API key and desired endpoint    HTTP.Request.Accept := 'application/json';    Response := HTTP.Get('https://newsapi.org/v2/top- headlines?country=us&apiKey=YOUR_API_KEY');     JSON := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(Response), 0) as TJSONObject;    if JSON <> nil then    begin      Articles := JSON.Get('articles') as TJSONArray;      if Articles <> nil then      begin        for i := 0 to Articles.Count - 1 do        begin        Article := Articles.Get(i) as TJSONObject;          Title := Article.Get('title').Value;          URL := Article.Get('url').Value;          // Process the title and URL          Writeln(Title);        Writeln(URL);        end;      end;      JSON.Free;    end;  finally  HTTP.Free;  end;  ReadLn;end. ---------------------------------------------------------------------- paule32 · 2025-07-03 18:24 wow, 1000 Lines more... why not working with a Database and holding one JSON structure ? so, you have multiple Records, and you can template the JSON structure. It is not a good idea to mix meta data with code data in one source. So, you have - a Executable for your business logic - b Database with your records (which can be more than 10.000 Lines) - c SQL Queries that can be inject into a) to read b) SQL Queries can be simple external SQL Files because you give the Text in a Text File. By doing this, you would realize a very faster performance instead using static JSON and Database SQL Structure's. ---------------------------------------------------------------------- CM630 · 2025-07-03 20:10 Quote from: Thaddy on July 03, 2025, 05:46:10 pm... There are no open issues regarding TRichmemo that I am aware of.... There is a bug tracker which is aware, I have filed a bug report in January, but nothing seems to be handles in the last 3 years. ---------------------------------------------------------------------- rvk · 2025-07-03 20:24 Quote from: CM630 on July 03, 2025, 10:10:59 pmQuote from: Thaddy on July 03, 2025, 05:46:10 pmQuote from: Nicole on July 03, 2025, 05:32:35 pm... There are no open issues regarding TRichmemo that I am aware of.... There is a bug tracker which is aware, I have filed a bug report in January, but nothing seems to be handles in the last 3 years.Your quoting seems to be wrong here... But I can't find any open issues in the bug tracker for TRichmemo. ---------------------------------------------------------------------- CM630 · 2025-07-04 06:06 Hm, are we talking about the same bug tracker? https://github.com/skalogryz/richmemo/issues Here is a bug report:  https://github.com/skalogryz/richmemo/issues/13 ---------------------------------------------------------------------- rvk · 2025-07-04 06:29 Quote from: CM630 on July 04, 2025, 08:06:52 amHm, are we talking about the same bug tracker? https://github.com/skalogryz/richmemo/issues Here is a bug report:  https://github.com/skalogryz/richmemo/issues/13 That's not the "bugtracker". The bugtracker is here https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues (See the link at the left of every forum-page which says bugtracker) Not sure if anyone sees the issue page you're pointing at. ---------------------------------------------------------------------- dsiders · 2025-07-04 06:40 Quote from: rvk on July 04, 2025, 08:29:36 amQuote from: CM630 on July 04, 2025, 08:06:52 amHm, are we talking about the same bug tracker? https://github.com/skalogryz/richmemo/issues Here is a bug report:  https://github.com/skalogryz/richmemo/issues/13 That's not the "bugtracker". The bugtracker is here https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues (See the link at the left of every forum-page which says bugtracker) Not sure if anyone sees the issue page you're pointing at. Bugs in TRichMemo would not be reported to the Lazarus bug tracker. It is a separate package, and not part of the LCL. ---------------------------------------------------------------------- rvk · 2025-07-04 07:27 Quote from: dsiders on July 04, 2025, 08:40:59 amBugs in TRichMemo would not be reported to the Lazarus bug tracker. It is a separate package, and not part of the LCL. Sure, but one shouldn't call the issue page of individual components "the bugtracker" here on the forum then ---------------------------------------------------------------------- Hansvb · 2025-07-08 18:00 As a reply to the first post.QuoteParsing to a string, - is even harder. I failed so many times and lost information and read error- messages... I also happen to be working on json and am trying out a few things beforehand. The code in the zip file is still very premature but the basics works. The json is put in a treeview. Completely copied from the json example that comes with Lazarus.The Json is put in a memo.The json is put in a stringgrid. (I will leave this I think)The json is put in a dataset so that this dataset can be linked to a dbgrid. (better option because I also want to sort and filter later). It is a trial/error created file, any comment lines are in Dutch. In the print screen you can see what it can do so far. This is the json from post 1 (slightly improved first). ---------------------------------------------------------------------- Thaddy · 2025-07-08 18:39 There is a jsonviewer in the tools section with full sourcecode. lazarus/tools/jsonviewer. ---------------------------------------------------------------------- Hansvb · 2025-07-08 18:47 I know: Quote Completely copied from the json example that comes with Lazarus. ---------------------------------------------------------------------- Thaddy · 2025-07-08 18:52 It is not one of the examples. It is a complete tool. That made me think you were not aware of it. The examples are separate. The examples are in fpc/packages/fcl-json/examples I also lift code from the tool.... Nit-picking, yes..