0

I'm trying to create a JSON file from an object in powershell, i can save it fine but when i try to load it from a python script i always get an error: ValueError: Expecting value: line 1 column 1 (char 0)

here's the powershell one-liner:

$devices = Get-WmiObject Win32_PNPEntity | where {$_.ConfigManagerErrorcode -ne 0}  | Select Name, CompatibleID | ConvertTo-Json | Out-File file.json

I belive it's because of the way powershell saves it, it looks like this:

[
    {
        "Name":  "PCI Data Acquisition and Signal Processing Controller",
        "CompatibleID":  [
                             "PCI\\VEN_8086\u0026DEV_A161\u0026REV_31",
                             "PCI\\VEN_8086\u0026DEV_A161",
                             "PCI\\VEN_8086\u0026CC_118000",
                             "PCI\\VEN_8086\u0026CC_1180",
                             "PCI\\VEN_8086",
                             "PCI\\CC_118000",
                             "PCI\\CC_1180"
                         ]
    },
    {
        "Name":  null,
        "CompatibleID":  [
                             "*PNP0CA0"
                         ]
    },
    {
        "Name":  "PCI Data Acquisition and Signal Processing Controller",
        "CompatibleID":  [
                             "PCI\\VEN_8086\u0026DEV_A127\u0026REV_31",
                             "PCI\\VEN_8086\u0026DEV_A127",
                             "PCI\\VEN_8086\u0026CC_118000",
                             "PCI\\VEN_8086\u0026CC_1180",
                             "PCI\\VEN_8086",
                             "PCI\\CC_118000",
                             "PCI\\CC_1180"
                         ]
    },
    {
        "Name":  "Base System Device",
        "CompatibleID":  [
                             "PCI\\VEN_8086\u0026DEV_1911\u0026REV_00",
                             "PCI\\VEN_8086\u0026DEV_1911",
                             "PCI\\VEN_8086\u0026CC_088000",
                             "PCI\\VEN_8086\u0026CC_0880",
                             "PCI\\VEN_8086",
                             "PCI\\CC_088000",
                             "PCI\\CC_0880"
                         ]
    },
    {
        "Name":  "PCI Device",
        "CompatibleID":  [
                             "PCI\\VEN_8086\u0026DEV_A135\u0026REV_31",
                             "PCI\\VEN_8086\u0026DEV_A135",
                             "PCI\\VEN_8086\u0026CC_000000",
                             "PCI\\VEN_8086\u0026CC_0000",
                             "PCI\\VEN_8086",
                             "PCI\\CC_000000",
                             "PCI\\CC_0000"
                         ]
    }
]

here's the python script:

import json
with open(r"C:\Users\azeleznx\PycharmProjects\Intel-Python-project\IT_Spider\scripts\file.json", 'r') as file:
    read = json.load(file)
    print(read)

and the error:

Traceback (most recent call last):
  File "C:/Users/Alex/PycharmProjects/Intel-Python-project/test21.py", line 3, in <module>
    read = json.load(file)
  File "C:\Python34\lib\json\__init__.py", line 268, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "C:\Python34\lib\json\__init__.py", line 318, in loads
    return _default_decoder.decode(s)
  File "C:\Python34\lib\json\decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python34\lib\json\decoder.py", line 361, in raw_decode
    raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)

now i know that JSON object should start and end with curly brackets, but i can't seem to make powershell save it the right way.

EDIT

Thanks Robᵩ for providing me with the answer, here's the complete script in as little lines as i

import subprocess
import json

command = "Write-Host (Get-WmiObject Win32_PNPEntity | where {$_.ConfigManagerErrorcode -ne 0}  " \
          "| Select Name, CompatibleID | ConvertTo-Json)"

out = json.loads(subprocess.check_output(["powershell.exe", '-ExecutionPolicy', 'Unrestricted', command]).decode("utf-8"))
print(out)
5
  • What are you using for loading the json? I tried loading the above one, and it worked for me Commented Jul 8, 2015 at 15:11
  • Try saving only an object, not an entire collection. Say you do $object.ToJSON(), instead do $object[0].toJSON(). Commented Jul 8, 2015 at 15:12
  • Thank you, Alex, for including the JSON text that is giving you trouble. Please also include the smallest complete Python program that demonstrates the error you are seeing. See stackoverflow.com/help/mcve for more info about complete examples. Commented Jul 8, 2015 at 15:49
  • "now i know that JSON object should start and end with curly brackets" - This is not true, according to RFC7158. Commented Jul 8, 2015 at 15:55
  • Edited post with additional information. Commented Jul 8, 2015 at 19:46

1 Answer 1

1

Here is my guess from incomplete information:

You've saved the file in powershell with a utf16 encoding, but you are opening the file in Python with a utf8 encoding.

Try specifying the correct encoding when you open the file in your Python script. How you do that depends upon whether you are using Python2 or Python3.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.