Converting multi-line text to JSON object with a key-value pair. Odd strings are keys; even strings are values.

String keys are converted automatically by following methods:

  1. The string begins with a small letter.
  2. All spaces and characters "-" are deleted, each new word that comes after the delimiter begins with a capital letter.

 

Example:

Host -> host

keep-alive -> keepAlive

Upgrade-Insecure-Requests -> upgradeInsecureRequests

Accept-Language -> acceptLanguage

 

Syntax

function TextWithCRLFToJSON(sText: string): string;

 

Parameters and return values

Parameter

Type

Value

sText

string

source string.

 

Function result

Returns the JSON object as a text string, separated by key-value pairs .

 

Example


const
  sHeaders =
    'Host'#13#10 +
    'mychat-server.com'#13#10 +
    'Connection'#13#10 +
    'keep-alive'#13#10 +
    'Upgrade-Insecure-Requests'#13#10 +
    '1'#13#10 +
    'User-Agent'#13#10 +
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'#13#10 +
    'Accept'#13#10 +
    'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'#13#10 +
    'Accept-Encoding'#13#10 +
    'gzip, deflate, sdch, br'#13#10 +
    'Accept-Language'#13#10 +
    'uk,ru;q=0.8,en-US;q=0.6,en;q=0.4'#13#10 +
    'Cookie'#13#10 +
    'io=LZl3ATHZ7MomZ50TAAAA';
var
  sJSONData, s, sList, sKey: string;
begin
  sJSONData := TextWithCRLFToJSON(sHeaders);
  
  sList := 'host,connection,upgradeInsecureRequests,userAgent,accept,acceptLanguage,cookie';
  
    while length(sList) > 0 do begin
      sKey := GetNextSt(sList, ',');
      
        if JSONGetString(sJSONData, sKey, s) = 0 then mLogScript(s, sKey);
    end;
end.


Script work result

[13:40:53] (Log "TextWithCRLFToJSON"): [host] mychat-server.com

[13:40:53] (Log "TextWithCRLFToJSON"): [connection] keep-alive

[13:40:53] (Log "TextWithCRLFToJSON"): [upgradeInsecureRequests] 1

[13:40:53] (Log "TextWithCRLFToJSON"): [userAgent] Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36

[13:40:53] (Log "TextWithCRLFToJSON"): [accept] text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

[13:40:53] (Log "TextWithCRLFToJSON"): [acceptLanguage] uk,ru;q=0.8,en-US;q=0.6,en;q=0.4

[13:40:53] (Log "TextWithCRLFToJSON"): [cookie] io=LZl3ATHZ7MomZ50TAAAA

[13:40:53] (Run "TextWithCRLFToJSON"): Script operation time: 6 ms

[13:40:53] (Run "TextWithCRLFToJSON"): Script done successfully.
 

See also

GetNextSt

JSONGetString

Length

mLogScript