javascriptjsonregexregex-groupaudacity

Convert text with tabs to a valide JSON with regex


I try to convert this plain txt exported from a audio sofware (audacity)

0.147652    0.983684    noing_grf
2.316547    3.609503    boing_4r4

To a valid json objet format like this enter image description here

{
    'noing_grf': { start: 0.147652, end: 0.983684 },
    'boing_4r4': { start: 2.316547, end: 3.609503 },
}

The pattern i try is this one ([^\t\n]+) But i think i need a full example to perform.

Any Regex pro can help me to do this, am not succeed at all ! My target it to export label and regions from the audio data track, than load in js and convert in a json format to manage spriteAudio like this API. http://pixijs.io/pixi-sound/examples/sprites.html


Solution

  • Do you have to use a regex to perform this? You could do this very simple in javascript (or in any other programming language).

    const data = `0.147652  0.983684    noing_grf
    2.316547    3.609503    boing_4r4`;
    
    const dictionary = {};
    const lines = data.split("\n");
    lines.forEach(line => {
        line = line.split("\t");
        dictionary[line[2]] = { start: line[0], end: line[1] };
    });
    
    

    the resulting dictionary would have the format you wanted:

    {
        noing_grf: { start: '0.147652', end: '0.983684' },
        boing_4r4: { start: '2.316547', end: '3.609503' }
    }