-1

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
8
  • json.loads(sys.argv[1]) would need sys.argv[1] being a JSON string (not a filename). Commented Apr 18, 2024 at 15:08
  • @tevemadar yes, I am passing a JSON string, not a file. And my JSON is valid so I am unsure why I am even getting the error. Commented Apr 18, 2024 at 15:10
  • 1
    Can you show exactly what your command line looks like? Passing a JSON structure (as a string) on a command line is going to be fraught with danger. You can start by print(sys.argv[1]) to see precisely what you're dealing with Commented Apr 18, 2024 at 15:32
  • your parameter is likely being stripped of double quotes but we can't know for sure unless you proved the exact command. Commented Apr 18, 2024 at 15:36
  • 1
    @JonSG yeah something is happening along the way becasue sys.argv[1] is not correct JSON Commented Apr 18, 2024 at 15:57

1 Answer 1

-2
import json

data = json.loads(original_json)

converted_data = {item["Name"]: item["File"] for item in data["Items"]}

converted_json = json.dumps(converted_data, indent=4)

print(converted_json)

Results from above Code

Sign up to request clarification or add additional context in comments.

3 Comments

In what sense is this an answer to the question?
it answers the second part for me. The first part is solving the error
Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.