Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
read json string as dict
#1
Hi,

I am trying to decode the string as dict.

>>> json_string
'{"ip": "13.1.6.0","data": {"tls": {"status": "io-timeout","protocol": "tls","timestamp": "2019-05-08T16:02:43+05:30","e
rror": "EOF"}}}{"ip": "13.1.6.0","data": {"tls": {"status": "unknown-error","protocol": "tls","timestamp": "2019-05-08T16:0
2:43+05:30","error": "tls: oversized record received with length 20527"}}}{"ip": "13.1.6.1","data": {"tls": {"status":
 "io-timeout","protocol": "tls","timestamp": "2019-05-08T16:02:43+05:30","error": "read tcp 13.1.6.0:60812-\\u003e13.1.6.0:443: read: connection reset by peer"}}}'
How can i decode the above string such that i can access like json_string["ip"], json_string["ip"]["tls"]

I tried using json.dumps as seen below

json_data = json.dumps(json_string)
>>> json_data["ip"]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    json_data["ip"]
TypeError: string indices must be integers, not str
please help. Sad
Reply
#2
well, this is not a valid json
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
sorry, there was a problem with my json format while reading content from file. thanks for pointing out.
Reply
#4
(May-08-2019, 12:50 PM)goron Wrote: sorry, there was a problem with my json format while reading content from file. thanks for pointing out.
if reading from file, why do you read in string? load directly from file into json_data.


import json

with open('sample.json') as json_file:
    json_data = json.load(json_file)

print(json_data)
Also, back to your snippet, when convert from string to json object json_data you would use json.loads(), not json.dumps()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formatting a date time string read from a csv file DosAtPython 5 1,253 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  [split] Parse Nested JSON String in Python mmm07 4 1,521 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Read nested data from JSON - Getting an error marlonbown 5 1,358 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Read JSON via API and write to SQL database TecInfo 5 2,188 Aug-09-2022, 04:44 PM
Last Post: TecInfo
  string indices must be integers when parsing Json ilknurg 3 6,347 Mar-10-2022, 11:02 AM
Last Post: DeaD_EyE
Question How do I skipkeys on json file read to python dictionary? BrandonKastning 3 1,885 Mar-08-2022, 09:34 PM
Last Post: BrandonKastning
Question convert unlabeled list of tuples to json (string) masterAndreas 4 7,459 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  Convert string to JSON using a for loop PG_Breizh 3 2,964 Jan-08-2021, 06:10 PM
Last Post: PG_Breizh
  String index out of bounds ( Python : Dict ) kommu 2 2,386 Jun-25-2020, 08:52 PM
Last Post: menator01
  TypeError: __repr__ returned non-string (type dict) shockwave 0 3,185 May-17-2020, 05:56 PM
Last Post: shockwave

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020