Python string to dictionary with json (not working)
<p>I have a text file like this :</p>

<p>"imei": "123456789",
"sim_no": "+90 xxx xxx xx xx",
"device_type": "standart",
"hw_version": "1.01",
"sw_version": "1.02"</p>

<p>and i want to convert to dictionary this file. Because i want to take values. </p>

<pre><code>import json
from time import sleep

def buffer(data):
    dicto=json.loads(data) 
    print(type(dicto))      

file=open("config.txt", "r").read()
jsondata=json.dumps(file)  
buffer(jsondata)

result : &lt;class 'str'&gt;
</code></pre>

<p>When i working in shell like this :</p>

<pre><code>&gt;&gt;&gt; import json
&gt;&gt;&gt; h = '{"foo":"bar", "foo2":"bar2"}'
&gt;&gt;&gt; type(h)
&lt;class 'str'&gt;
&gt;&gt;&gt; d=json.loads(h)
&gt;&gt;&gt; d
{'foo2': 'bar2', 'foo': 'bar'}
&gt;&gt;&gt; type(d)
&lt;class 'dict'&gt;
&gt;&gt;&gt; 
</code></pre>

<p>its working but i can't understand why my code not working. When i convert this file to dictionary i want to hold in a buffer. How can i hold this data inside array? Please excuse me i am new in Python.</p>

<p>Seems like you could just add the outer curly braces JSON requires:</p>

<pre><code>with open('config.txt', 'r+') as f:
    newdata = '{{ {} }}'.format(f.read())
    f.seek(0)
    f.write(newdata)
</code></pre>

<p>Otherwise, the file is already JSON, so no actual use of the <code>json</code> module is required (unless you want to check if it's already legal before modifying, or verify legality after).</p>

python json python-3.x