Home > Mobile >  Python script to import json file as a string and parse
Python script to import json file as a string and parse

Time:04-28

I have a python script below that i know works when i have a json string in the second line. The question is how can i import the data from a json file (output.json) as a string into the second line?

# JSON string

product = ' {contents of a json file go here} '

# Sanitize data
product = product.replace('\n', '').replace('\r', '')  

# Convert string to Python dict
product_dict = json.loads(str(product))

findd = []

fa = product_dict['hits']['hits']
for every_item in fa:
    findd.append(every_item['_source']['securityId'])

print(findd)

So far I have this....

import json

# Opening JSON file
with open('output.json', 'wb') as product:
     json.dumps(product)

print (product)

# Convert string to Python dict

product_dict = json.loads(str(product))

# Sanitize data
#product = product.replace('\n', '').replace('\r', '')

findd = []

fa = product_dict['hits']['hits']
for every_item in fa:
    findd.append(every_item['_source']['securityId'])

print(findd)

Which give this error:

Traceback (most recent call last):   File "/home/fxeduat/staticdatacheck1.py", line 5, in <module>
    json.dumps(product)   File "/opt/tech/releases/tools/python/current/lib/python3.9/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)   File "/opt/tech/releases/tools/python/current/lib/python3.9/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)   File "/opt/tech/releases/tools/python/current/lib/python3.9/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)   File "/opt/tech/releases/tools/python/current/lib/python3.9/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type BufferedWriter is not JSON serializable

So now i have

import json

# Opening JSON file
with open('output.json') as product:

# Convert string to Python dict
 product_dict = json.loads(str(product))

# Sanitize data
product = product.replace('\n', '').replace('\r', '')

findd = []

fa = product_dict['hits']['hits']
for every_item in fa:
    findd.append(every_item['_source']['securityId'])

print(findd)

however this errors

 Traceback (most recent call last):
      File "/home/fxeduat/staticdatacheck1.py", line 7, in <module>
        product_dict = json.loads(str(product))
      File "/opt/tech/releases/tools/python/current/lib/python3.9/json/__init__.py", line 346, in loads
        return _default_decoder.decode(s)
      File "/opt/tech/releases/tools/python/current/lib/python3.9/json/decoder.py", line 337, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/opt/tech/releases/tools/python/current/lib/python3.9/json/decoder.py", line 355, in raw_decode
        raise JSONDecodeError("Expecting value", s, err.value) from None
    json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

does the json file need to be elsewhere?

@Matthias

import json

# Opening JSON file
with open('output.json') as f:
 product = json.load(f)
 print(product)

# Convert string to Python dict
product_dict = json.loads(str(product))

# Sanitize data
product = product.replace('\n', '').replace('\r', '')

findd = []

fa = product_dict['hits']['hits']
for every_item in fa:
    findd.append(every_item['_source']['securityId'])

print(findd)

errors

Traceback (most recent call last):
  File "/home/fxeduat/staticdatacheck1.py", line 5, in <module>
    product = json.load(f)
  File "/opt/tech/releases/tools/python/current/lib/python3.9/json/__init__.py", line 293, in load
    return loads(fp.read(),
  File "/opt/tech/releases/tools/python/current/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/opt/celertech/releases/tools/python/current/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/opt/tech/releases/tools/python/current/lib/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

@quamrana

import json

# Opening JSON file
with open('output.json') as product:
     product_dict = json.load(product)

# Sanitize data
product = product.replace('\n', '').replace('\r', '')

findd = []

fa = product_dict['hits']['hits']
for every_item in fa:
    findd.append(every_item['_source']['securityId'])

print(findd)

new error

Traceback (most recent call last):
  File "/home/fxeduat/staticdatacheck1.py", line 13, in <module>
    product = product.replace('\n', '').replace('\r', '')
AttributeError: '_io.TextIOWrapper' object has no attribute 'replace'

CodePudding user response:

If you have the already existing file output.json, then you can use the json library to read that file:

import json

# Opening JSON file
with open('output.json') as product:
     product_dict = json.load(product)

fa = product_dict['hits']['hits']
...

CodePudding user response:

Answer

import json

# Opening JSON file
with open('output.json') as product:
         product_dict = json.load(product)


findd = []

fa = product_dict['hits']['hits']
for every_item in fa:
    findd.append(every_item['_source']['securityId'])

print(findd)
  • Related