.txt write to .json file batch or php file needed

Viewed 1054

i need and search for a result to convert my .txt file to a .json file with that format:

{
"id":"12777475756802056",
"typ":"Solo",
"match":
    {
        "rank":"1",
        "playeruserid":"165496871657",
        "playername":"Example Name",
        "kills":"8",
        "points":"224000",
        "killer":"empty", // while firstplace
        "weapon":"empty" // while firstplace
    },
    {
        "rank":"2",
        "playeruserid":"654987654984",
        "playername":"Example Name 2",
        "kills":"4",
        "points":"168000",
        "killer":"Example Name",
        "weapon":"Shotgun"
    }
    ... another players here.
}

and my .txt file looks so, yep really bad... with so many spaces and all in one line...: https://pastebin.com/FP0C9BCj

i try this batch to remove the many spaces:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION 
SET "sourcedir=D:\Users\Raphael\Desktop"
SET "destdir=D:\Users\Raphael\Desktop"
SET "filename1=%sourcedir%\matchreport.txt"
SET "outfile=%destdir%\outfile.txt"
(
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
 SET "line=%%a"
 SET "line=!line:?=!"
 SET "line=!line:/=-!"
 SET "line=!line::=!"
 SET "line=!line: =;!"
 ECHO !line!
)
)>"%outfile%"

GOTO :EOF

and idk now why can convert this now to a json file, while all informations on one line pro player, and the json format is now:

{"1. 5605****11014 Cl***ma  205000 + 19000 ( 8) = 224000"}

and sorry this is my first try to convert a txt to a json file have u any idea to convert this? :/

3 Answers

Although primarily a html/xml/json data extraction tool, Xidel can also process raw text with a little XPath/XQuery magic.

I'm using dbenham's pseudo-fixed 'test.txt' as input

Match ID: 6549999999997461, Solo
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Rank   GameUserId           Name                       Rank   Kills (##)    Total   Killer                    Weapon
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  1.   5605999911014        Claaama                  205000 + 19000 ( 8) = 224000                             m4                              
  2.   2384449999020        Apaaaaaaaift             172403 + 11550 ( 3) = 183953   Claaama                   m4            
  3.   929999999515         Swaa aUK                 156259 + 14900 ( 6) = 171159   Apaaaaaaaift              m4            
  4.   6583833999132        Mocaaaaaaaakap           144805 +  2000 ( 1) = 146805   Claaama                   shotgun            
  5.   6219997360388        Olaaaraaa                135920 +  6200 ( 3) = 142120   Apaaaaaaaift              m4      
  6.   1896619999980        Opaaaaaaaaagon           128661 +     0 ( 0) = 128661   SwaaaaUK                  m4            
  7.   6408999979452        Maaaa aaanner            122523 +  3500 ( 2) = 126023   Swaa aUK                  shot gun              
  8.   590609992163         Aaaag                    117207 +     0 ( 0) = 117207   Apaaaaaaaift              ak      
  9.   8314679999599        aaaaaaaaangstar          112517 +     0 ( 0) = 112517   Olaaaraaa                 shotgun                  
 10.   3454299997961        aaaaaaaaReally           108322 +  5000 ( 3) = 113322   [Toxic Gas]                                              
 11.   9049999958750        aaaa atch                104528 +  3500 ( 2) = 108028   Swaa aUK                  shot gun
xidel.exe -s test.txt -e "let $line:=tokenize($raw,'\r\n'),$match:=extract($line[1],'^Match ID: (\d+), (.+)',(1,2)) return {'id':$match[1],'type':$match[2],'match':$line[position()>4] ! (let $obj:=extract(.,'^ *(\d+)\. +(\d+) +(.*?\S) .+\( ?(\d+)\) = (\d+) {3}(.*?\S?.*?)?  +($|.*\S)',1 to 7) return {'rank':$obj[1],'playeruserid':$obj[2],'playername':$obj[3],'kills':$obj[4],'points':$obj[5],'killer':$obj[6],'weapon':$obj[7]})}"

or a bit more readable (prettyprinted):

xidel.exe -s test.txt -e ^" ^
  let $line:=tokenize( ^
        $raw, ^
        '\r\n' ^
      ), ^
      $match:=extract( ^
        $line[1], ^
        'Match ID: (\d+), (.+)', ^
        (1,2) ^
      ) ^
  return { ^
    'id':$match[1], ^
    'type':$match[2], ^
    'match':$line[position()^>4] ! ( ^
      let $obj:=extract( ^
        ., ^
        '^^ *(\d+)\. +(\d+) +(.*?\S) .+\( ?(\d+)\) = (\d+) {3}(.*?\S?.*?)?  +($^|.*\S)', ^
        1 to 7 ^
      ) return { ^
        'rank':$obj[1], ^
        'playeruserid':$obj[2], ^
        'playername':$obj[3], ^
        'kills':$obj[4], ^
        'points':$obj[5], ^
        'killer':$obj[6], ^
        'weapon':$obj[7] ^
      } ^
    ) ^
  }^"

(the end of each line, as well as ",>,^,| needs to be escaped with a ^)

After opening 'test.txt' we first create 2 internal variables; $line and $match.
$line becomes a sequence of individual lines. $match becomes a sequence of the Match ID and the type by extracting this from the first line of the input.

After assigning these 2 internal variables we begin to create the json. The id attribute gets the Match ID is value, the type attribute 'Solo'.
Next we create the match array and start processing line 5 and up. Each line we feed to the extract()-function to create another internal variable (dbenham's regex slightly altered). This variable holds a sequence (7 items) of all the individual values we're looking for. And with this variable we finally populate the object, where we assign the rank attribute the first item, playeruserid attribute the second item, etc.

The output:

{
  "id": "6549999999997461",
  "type": "Solo",
  "match": [
    {
      "rank": "1",
      "playeruserid": "5605999911014",
      "playername": "Claaama",
      "kills": "8",
      "points": "224000",
      "killer": "",
      "weapon": "m4"
    },
    {
      "rank": "2",
      "playeruserid": "2384449999020",
      "playername": "Apaaaaaaaift",
      "kills": "3",
      "points": "183953",
      "killer": "Claaama",
      "weapon": "m4"
    },
    {
      "rank": "3",
      "playeruserid": "929999999515",
      "playername": "SwaaaaUK",
      "kills": "6",
      "points": "171159",
      "killer": "Apaaaaaaaift",
      "weapon": "m4"
    },
    {
      "rank": "4",
      "playeruserid": "6583833999132",
      "playername": "Mocaaaaaaaakap",
      "kills": "1",
      "points": "146805",
      "killer": "Claaama",
      "weapon": "shotgun"
    },
    {
      "rank": "5",
      "playeruserid": "6219997360388",
      "playername": "Olaaaraaa",
      "kills": "3",
      "points": "142120",
      "killer": "Apaaaaaaaift",
      "weapon": "m4"
    },
    {
      "rank": "6",
      "playeruserid": "1896619999980",
      "playername": "Opaaaaaaaaagon",
      "kills": "0",
      "points": "128661",
      "killer": "SwaaaaUK",
      "weapon": "m4"
    },
    {
      "rank": "7",
      "playeruserid": "6408999979452",
      "playername": "Maaaaaaaanner",
      "kills": "2",
      "points": "126023",
      "killer": "SwaaaaUK",
      "weapon": "shotgun"
    },
    {
      "rank": "8",
      "playeruserid": "590609992163",
      "playername": "Aaaag",
      "kills": "0",
      "points": "117207",
      "killer": "Apaaaaaaaift",
      "weapon": "ak"
    },
    {
      "rank": "9",
      "playeruserid": "8314679999599",
      "playername": "aaaaaaaaangstar",
      "kills": "0",
      "points": "112517",
      "killer": "Olaaaraaa",
      "weapon": "shotgun"
    },
    {
      "rank": "10",
      "playeruserid": "3454299997961",
      "playername": "aaaaaaaaReally",
      "kills": "3",
      "points": "113322",
      "killer": "[Toxic Gas]",
      "weapon": ""
    },
    {
      "rank": "11",
      "playeruserid": "9049999958750",
      "playername": "aaaaaatch",
      "kills": "2",
      "points": "108028",
      "killer": "SwaaaaUK",
      "weapon": "shotgun"
    }
  ]
}
Related