Home > Mobile >  i want to convert text file to json
i want to convert text file to json

Time:08-15

mydata =

Reason: RSA private key
Date: 2021-05-10 01:37:04
Hash: 55cd03eb0f76c7b23d6c53dd2555f83709f834
Filepath: labs/lab-files/open-ssl-lab-files/private-key.pem
Branch: origin/master
Commit: 2.8.45: Updated Docker Files for PHP 8
-----BEGIN RSA PRIVATE KEY-----

Reason: Password in URL
Date: 2018-09-28 08:18:39
Hash: 0f9d4a66f2540f5154c01476e811615aad6291
Filepath: includes/hints/sqlmap-hint.inc
Branch: origin/master
Commit: =2.6.67
mysql://root:""@192.168.56.102:5123/nowasp

Reason: Password in URL
Date: 2018-09-28 08:18:39
Hash: 0f9d4a66f2540f5154c01476e811615aad6291
Filepath: owasp-esapi-php/lib/simpletest/docs/en/authentication_documentation.html
Branch: origin/master
Commit: =2.6.67
http://<strong>Me:Secret@</strong>www.lastcraft.com/protected/')

output should be =

[
{
'Reason': 'RSA private key'
'Date': '2021-05-10 01:37:04'
'Hash': '55cd03eb0f76c7b23d6c53dd2555f83709f834'
'Filepath': 'labs/lab-files/open-ssl-lab-files/private-key.pem'
'Branch': 'origin/master'
'Commit': '2.8.45: Updated Docker Files for PHP 8'
'url':'-----BEGIN RSA PRIVATE KEY-----'}
{
'Reason: 'Password in URL'
'Date': '2018-09-28 08:18:39'
'Hash': '0f9d4a66f2540f5154c01476e811615aad6291'
'Filepath': 'includes/hints/sqlmap-hint.inc'
'Branch': 'origin/master'
'Commit': '=2.6.67'
'url':'mysql://root:&quot;&quot;@192.168.56.102:5123/nowasp
}
{
'Reason': 'Password in URL'
'Date': '2018-09-28 08:18:39'
'Hash': '0f9d4a66f2540f5154c01476e811615aad6291'
'Filepath':'owasp-esapi-php/lib/simpletest/docs/en/authentication_documentation.html'
'Branch': 'origin/master'
'Commit': '=2.6.67'
'url':'http://<strong>Me:Secret@</strong>www.lastcraft.com/protected/');'
}

I want to convert data to json

my code is

h = []
dy = {}
for index,name in  enumerate(thy):
    if (bool(re.search('://',name))) == True:
        dy.update({'url':name})
        continue
    elif (bool(re.search('-----BEGIN RSA',name))) == True:
        dy.update({'url':name})
        continue
    elif index % 6 == 0:
        h.append(dy)
        print(dy)
    else:
        a = name.split(':',1)[0]
        b = name.split(':',1)[1]
        dy.update({a:b})

but output is wrong can any one solve this

CodePudding user response:

Much of your code is missing like what thy actually is and how you read your file or serialise to JSON.

But even without this information, this is the comments I have for your code:

  • if xxx == True is wrong, if already checks that the condition evaluates to True (which like False and None, are checked with is a they are singletons)
  • converting a re.search to bool this way bad practice, firstly because I don't think re.search is appropriate in this context (re.match would be better), second because you should use the underlying condition the bool convertion uses
  • you never seem to reset the dict dy, which would ensure it's clean between two blocks
  • you split the line twice on colon, couldn't you just do it once ?

So you should check more of the documentation for all the things you use.


In any case, I made my own version which you can compare against.

kv_line = re.compile(r"^(\w ): (.*)$")

h = []

with open(in_file, "rt") as f:
    d = {}
    for line in f:
        line = line.rstrip()
        m = kv_line.match(line)
        if m:
            d[m[1]] = m[2]
        else:
            if not line:
                continue
            d["url"] = line
            h.append(d)
            d = {}

json.dumps(h)

which gives

[
    {
        "Reason": "RSA private key",
        "Date": "2021-05-10 01:37:04",
        "Hash": "55cd03eb0f76c7b23d6c53dd2555f83709f834",
        "Filepath": "labs/lab-files/open-ssl-lab-files/private-key.pem",
        "Branch": "origin/master",
        "Commit": "2.8.45: Updated Docker Files for PHP 8",
        "url": "-----BEGIN RSA PRIVATE KEY-----"
    },
    {
        "Reason": "Password in URL",
        "Date": "2018-09-28 08:18:39",
        "Hash": "0f9d4a66f2540f5154c01476e811615aad6291",
        "Filepath": "includes/hints/sqlmap-hint.inc",
        "Branch": "origin/master",
        "Commit": "=2.6.67",
        "url": "mysql://root:&quot;&quot;@192.168.56.102:5123/nowasp"
    },
    {
        "Reason": "Password in URL",
        "Date": "2018-09-28 08:18:39",
        "Hash": "0f9d4a66f2540f5154c01476e811615aad6291",
        "Filepath": "owasp-esapi-php/lib/simpletest/docs/en/authentication_documenta
tion.html",
        "Branch": "origin/master",
        "Commit": "=2.6.67",
        "url": "http://<strong>Me:Secret@</strong>www.lastcraft.com/protected/"
    }
]

CodePudding user response:

You could try to do it without use of re.
If your data is like here:

my_data = """Reason: RSA private key
Date: 2021-05-10 01:37:04
Hash: 55cd03eb0f76c7b23d6c53dd2555f83709f834
Filepath: labs/lab-files/open-ssl-lab-files/private-key.pem
Branch: origin/master
Commit: 2.8.45: Updated Docker Files for PHP 8
-----BEGIN RSA PRIVATE KEY-----

Reason: Password in URL
Date: 2018-09-28 08:18:39
Hash: 0f9d4a66f2540f5154c01476e811615aad6291
Filepath: includes/hints/sqlmap-hint.inc
Branch: origin/master
Commit: =2.6.67
mysql://root:&quot;&quot;@192.168.56.102:5123/nowasp

Reason: Password in URL
Date: 2018-09-28 08:18:39
Hash: 0f9d4a66f2540f5154c01476e811615aad6291
Filepath: owasp-esapi-php/lib/simpletest/docs/en/authentication_documentation.html
Branch: origin/master
Commit: =2.6.67
http://<strong>Me:Secret@</strong>www.lastcraft.com/protected/')"""

then this:

i = -1
sections_lst = []
sections_dct = {}
sections = my_data.split('\n\n')
for s in sections:
    i  = 1
    sections_dct[i] = s.split('\n')
    for r in sections_dct[i]:
        if r[ :5 ] in('-----', 'http:', 'mysql'):
            r = 'url: '   r
        r = r[ :r.find(': ')   2 ]   ', '   r[ r.find(': ')   2: ]
        r = r.split(': , ')
        sections_lst.append(r)
    sections_dct[i] = sections_lst
    sections_lst = []
      
my_list = []
my_dct = {} 
for lst in sections_dct.values():
    for item in lst:
        my_dct[item[0]] = item[1]
    my_list.append(my_dct)
    my_dct = {}

results as:

#    R e s u l t 
my_list = 
[   {   'Reason': 'RSA private key', 
        'Date': '2021-05-10 01:37:04', 
        'Hash': '55cd03eb0f76c7b23d6c53dd2555f83709f834', 
        'Filepath': 'labs/lab-files/open-ssl-lab-files/private-key.pem', 
        'Branch': 'origin/master', 
        'Commit': '2.8.45: Updated Docker Files for PHP 8', 
        'url': '-----BEGIN RSA PRIVATE KEY-----'
    }, 
    {   'Reason': 'Password in URL', 
        'Date': '2018-09-28 08:18:39', 
        'Hash': '0f9d4a66f2540f5154c01476e811615aad6291', 
        'Filepath': 'includes/hints/sqlmap-hint.inc', 
        'Branch': 'origin/master', 
        'Commit': '=2.6.67', 
        'url': 'mysql://root:&quot;&quot;@192.168.56.102:5123/nowasp'
    }, 
    {   'Reason': 'Password in URL', 
        'Date': '2018-09-28 08:18:39', 
        'Hash': '0f9d4a66f2540f5154c01476e811615aad6291', 
        'Filepath': 'owasp-esapi-php/lib/simpletest/docs/en/authentication_documentation.html', 
        'Branch': 'origin/master', 
        'Commit': '=2.6.67', 
        'url': "http://<strong>Me:Secret@</strong>www.lastcraft.com/protected/')"
    }
]

  • Related