I have an automation that runs and passes a JSON object to a Python script.
My objective is to read the JSON and convert it to a dictionary.
My JSON looks like this:
{
"Items": [
{
"Name": "baz",
"File": "\\\\baz\\baz\\baz baz\\baz baz\\baz\\baz.xls"
},
{
"Name": "bar",
"File": "\\\\bar\\bar\\bar bar\\bar bar\\bar\\bar.csv"
},
{
"Name": "foo",
"File": "\\\\foo\\foo\\foo foo\\foo foo\\foo\\foo.csv"
}
]
}
I need it to look like this:
{
"foo" : "\\\\foo\\foo\\foo foo\\foo foo\\foo\\foo.csv",
"bar" : "\\\\bar\\bar\\bar bar\\bar bar\\bar\\bar.csv",
"baz" : "\\\\baz\\baz\\baz baz\\baz baz\\baz\\baz.xls"
}
I get this error with this piece of code:
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 4 (char 6)
if len(sys.argv) > 1:
d = json.loads(sys.argv[1])
print(d)
This is what I pass to PowerShell:
@"
{
{
"Items": [
{
"Name": "baz",
"File": "\\\\baz\\baz\\baz baz\\baz baz\\baz\\baz.xls"
},
{
"Name": "bar",
"File": "\\\\bar\\bar\\bar bar\\bar bar\\bar\\bar.csv"
},
{
"Name": "foo",
"File": "\\\\foo\\foo\\foo foo\\foo foo\\foo\\foo.csv"
}
]
}
}
"@
& $Python $Script $json
I printed sys.argv[1]:
{Items:[{Name:foo,File:\\\\foo\\foo\\foo
{Items:[{Name:foo,File:\\\\foo\\foo\\foo
json.loads(sys.argv[1])would needsys.argv[1]being a JSON string (not a filename).