Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can i parse my output?
#1
I have this code.

import json

with open('data5.json', 'r') as myfile:
    data = myfile.read()
    data_2 = data.replace('[', ' ')
    data_3 = data_2.replace(']', ' ')
    print(data_3)
My variable data_3 is like that:


  {"Manufacturer": "VMware, Inc.", "Model": "VMware7,1", "Name": "DC01"} ,  {"Index": "1", "IPAddress":  "192.168.1.240,fe80::350e:d28d:14a5:5cbb" } 
I want to parse this. I want to get the value of Manufacturer or Model or Name. How can i do it?
Reply
#2
Why do you import json if you don't use json.load() or loads() ? Module json is the json parser included in python.
Reply
#3
ı tried to use json load

import json
with open('data6.json', 'r') as myfile:
    data = json.load(myfile)
and data6.json is like that

{"Manufacturer": "VMware, Inc.", "Model": "VMware7,1", "Name": "DC01"} ,  {"Index": "1", "IPAddress":  "192.168.1.240,fe80::350e:d28d:14a5:5cbb" }
but it gives the error

Traceback (most recent call last):
  File "C:\Users\DELL\AppData\Roaming\JetBrains\PyCharmCE2021.3\scratches\scratch_38.py", line 3, in <module>
    data = json.load(myfile)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 72 (char 71)

Process finished with exit code 1
Reply
#4
Sure, that the square-brackets are missing in the JSON file? If yes, then your file is malformed and could not parse without modification. The JSON module of the Python standard lib is very strict.

If this is the content of your input file:
{"Manufacturer": "VMware, Inc.", "Model": "VMware7,1", "Name": "DC01"} ,  {"Index": "1", "IPAddress":  "192.168.1.240,fe80::350e:d28d:14a5:5cbb" }
Then you may add [ and ] to the str.
# first you just read your json file
# here I just put the raw str into the code, because of laziness...
data_as_text = """{"Manufacturer": "VMware, Inc.", "Model": "VMware7,1", "Name": "DC01"} ,  {"Index": "1", "IPAddress":  "192.168.1.240,fe80::350e:d28d:14a5:5cbb" }"""

# parsing
my_data = json.loads("[" + data_as_text + "]")
But it's better to have valid JSON content.

Sometimes people add line by line JSON data.
This is also not parsable with json because it's not valid.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
The parser fails because data6.json is not valid JSON syntax. Where do you get this file from ? I suggest to try with data5.json instead.
Reply
#6
I get your suggessions. I have 3 vmi commands and there are the result of its.

[{'Manufacturer': 'VMware, Inc.', 'Model': 'VMware7,1', 'Name': 'DC01'}]
[{'Index': '1', 'IPAddress': ['192.168.1.240,fe80::350e:d28d:14a5:5cbb']}] 
[{'Name': 'DC01', 'UserName': None}]
They are seperate strings. I create an output_array and appent all these 3 lines into it.
So the output_array is :

[[{'Manufacturer': 'VMware, Inc.', 'Model': 'VMware7,1', 'Name': 'DC01'}], [{'Index': '1', 'IPAddress': ['192.168.1.240,fe80::350e:d28d:14a5:5cbb']}], [{'Name': 'DC01', 'UserName': None}]] 
For parsing it, im trying to write this content in a json file and i tried read and parse it.
When i use json validator it says that it is a valid json.
Now i tried to run this code

import json
with open('data5.json', 'r') as myfile:
    data = json.load(myfile)
    for i in data:
        print(data['Name'])
And again getting the error

Traceback (most recent call last):
  File "C:\Users\DELL\AppData\Roaming\JetBrains\PyCharmCE2021.3\scratches\scratch_38.py", line 5, in <module>
    print(data['Name'])
TypeError: list indices must be integers or slices, not str
I want to reach this values
Name
Manucaturer
Username.


i'm stuck here
Reply
#7
Hm... what if all elements of the 3 lists are packed into one dict?

(Mar-10-2022, 11:09 AM)ilknurg Wrote: [{'Manufacturer': 'VMware, Inc.', 'Model': 'VMware7,1', 'Name': 'DC01'}]
[{'Index': '1', 'IPAddress': ['192.168.1.240,fe80::350e:d28d:14a5:5cbb']}]
[{'Name': 'DC01', 'UserName': None}]

The single quotes are invalid for use in JSON.
I look more like Python Code.
You could use ast.literal_eval, which is safe.


from ipaddress import ip_address
from ast import literal_eval


def parse(file):
    result = {}

    with open("data.txt") as fd:
        for line in fd:
            # skipping empty lines with only
            # whitespace
            if not line.strip():
                continue

            for element in literal_eval(line):
                result |= element

    # example to change existing data on result
    # list contains 1 str with many ip addresses
    # the addresses are splitted by a comma
    if "IPAddress" in result:
        ip_addresses = []

        # get the list from IPAddresses
        # and take the first element (there is only one)
        # then split this str by comma
        for ip in result["IPAddress"][0].split(","):
            # creating a IPv4Address or IPv6Address instance from
            # the str of the ip address. Not required
            ip_addresses.append(ip_address(ip))
            # to append only the str
            # ip_address.append(ip)

        # assign the ip_addresses
        result["IPAddress"] = ip_addresses

    return result
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
Thank you for your answer.
In my database, i have a column which name is Manufacturer.

I want to get Manufacturer info from this line, and save it to in my db.


{'Manufacturer': 'VMware, Inc.', 'Model': 'VMware7,1', 'Name': 'DC01', 'Index': '1', 'IPAddress': ['192.168.1.240,fe80::350e:d28d:14a5:5cbb'], 'UserName': None}
How can i extract only the manufacturer? I want to parse it
Reply
#9
I changed my code to read a file, and before I didn't see that the list with the IPAddress does contain only one element. The first element contains all addresses separated by a comma.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#10
data = {'Manufacturer': 'VMware, Inc.', 'Model': 'VMware7,1', 'Name': 'DC01', 'Index': '1', 'IPAddress': ['192.168.1.240,fe80::350e:d28d:14a5:5cbb'], 'UserName': None}

print(f"Manufacturer: {data['Manufacturer']}")
Output:
Manufacturer: VMware, Inc.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can i parse my output? ilknurg 4 1,581 Mar-16-2022, 03:27 PM
Last Post: snippsat
  Unable to parse JSON output dragan979 1 3,560 Apr-20-2018, 02:24 PM
Last Post: dragan979

Forum Jump:

User Panel Messages

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