Home > Mobile >  Dumping a JSON array to Python doesn't work
Dumping a JSON array to Python doesn't work

Time:10-26

When I try to dump a JSON array from a file as a list in Python it gives me a string. Instead, I want to get a list to be able to modify it.

The code:

import json
hola = []
f = open("prueba.json", "r")
file_data = json.loads(f.read())

print(file_data)

The content of the JSON file (prueba.json):

"[\"sbs\", \"sb\", \"bs\", \"v\", \"\"]"

The output:

["sbs", "sb", "bs", "v", ""]

But, when I put the same content in a variable, it works perfectly fine and it returns a list.

The code:

import json
hola = []
f = "[\"sbs\", \"sb\", \"bs\", \"v\", \"\"]"
file_data = json.loads(f)

print(file_data)

The output:

['sbs', 'sb', 'bs', 'v', '']

How can I make the first case return me a list, instead of a string?

CodePudding user response:

But, when I put the same content in a variable, it works perfectly fine and it returns a list.

Because, that isn't the same content as the file. I created a file with the content you describe:

import json

with open('prueba.json') as f:
    data = f.read()
print(data)

f = "[\"sbs\", \"sb\", \"bs\", \"v\", \"\"]"
print(f == data, f)   # (False) no this isn't the same content

f2 = r'"[\"sbs\", \"sb\", \"bs\", \"v\", \"\"]"'
print(f2 == data, f2) # (True) f2 is the same file content.  Need the quotes
                      # and literal backslashes, hence use of raw string

# Your data is a JSON dump of string created by a JSON dump (double encoded)
file_data = json.loads(data)      # decode once
print(type(file_data),file_data)  # So you get a string 

fixed = json.loads(file_data)     # decode again
print(type(fixed),fixed)          # correct now (list)

Output:

"[\"sbs\", \"sb\", \"bs\", \"v\", \"\"]"
False ["sbs", "sb", "bs", "v", ""]
True "[\"sbs\", \"sb\", \"bs\", \"v\", \"\"]"
<class 'str'> ["sbs", "sb", "bs", "v", ""]
<class 'list'> ['sbs', 'sb', 'bs', 'v', '']

Or more directly:

import json

with open('prueba.json') as f:
    data = json.loads(json.load(f))
print(type(data),data)

# <class 'list'> ['sbs', 'sb', 'bs', 'v', '']
  • Related