Writting string to a File - 'str' object is not callable - python

i am generating a Json-like structure and I need write it to a File, any format of file, but i am getting this error.
I have tried written it as Json and string without any luck.
import csv
score = []
test = ''
with open("score_test.txt") as f:
spam = csv.reader(f, delimiter='|')
for line in spam:
score.append(line)
open = """{
"info": {
"_postman_id": "",
"name": "GD-API-Testing-Update",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"name": "Score_Tests",
"item": [
{
"name": "Score_Ok",
"item": ["""
for num, row in enumerate(score):
test += """
{
"name": "Score_Ok_%d",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"Status test of API-GD.\", function () {",
" pm.response.to.have.status(200);",
"});",
"",
"pm.test(\"Score test.\", function () {",
" pm.response.to.not.be.error;",
" const responseJson = pm.response.json();",
" pm.expect(responseJson.gd_result).to.eql("%s");",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [
{
"key": "X-Access-Key-Id",
"value": "{{X-Access-Key-Id}}",
"type": "text"
},
{
"key": "X-Secret-Access-Key",
"value": "{{X-Secret-Access-Key}}",
"type": "text"
},
{
"key": "Accept-Version",
"value": "{{Accept-Version}}",
"type": "text"
}
],
"url": {
"raw": "{{URL-API-GD}}/api/model/:model_id/mdn/:mdn",
"host": [
"{{URL-API-GD}}"
],
"path": [
"api",
"model",
":model_id",
"mdn",
":mdn"
],
"variable": [
{
"key": "model_id",
"value": "{{Test-Model}}"
},
{
"key": "mdn",
"value": "%s"
}
]
}
},
"response": []
}""" % (num, row[1], row[0])
if num != len(score) - 1:
test += ","
close = """ ]
}
]
}"""
sample = open + test + close
with open("sample.txt", "a") as f:
f.write(sample)
This is the error:
Traceback (most recent call last):
File "/Users/user/Documents/training/TestForPostman/testGenerator.py", line 111, in <module>
with open("sample.txt", "a") as f:
TypeError: 'str' object is not callable
The for Loop it is appending the generated string I need, and finally closed the structure at the end.
I already checked the json structure and it is ok.

You are using open as variable in your code sample = open + test + close which is causing issue. Avoid using any reserved word as a variable.
Rename the open variable with something else then it will work.

Related

How to delete an element of an array in a JSON file by its key in Python?

I am creating a kind-of database in using the .JSON file and I want to delete a specific element of an array in a JSON file using Python language, but I can't do this how I want, here's what info.json file looks like:
{
"dates": [
{
"date": "10/10",
"desc": "test1"
},
{
"date": "09/09",
"desc": "test3"
}
],
"data": [
{
"name": "123",
"infotext": "1234"
},
{
"name": "!##",
"infotext": "!##$"
}
]
}
Here's what json_import.py file looks like:
def delete_data():
name = input("Enter data name\n")
with open("info.json", "r+") as f:
file_data = json.load(f)
for x in file_data["data"]:
if x["name"] == name:
file_data["data"].remove(x)
f.seek(0)
json.dump(file_data, f, indent = 4)
delete_data()
TERMINAL:
Enter data name
!##
Expected:
{
"dates": [
{
"date": "10/10",
"desc": "test1"
},
{
"date": "09/09",
"desc": "test3"
}
],
"data": [
{
"name": "123",
"datatext": "1234"
}
]
}
Actual result:
{
"dates": [
{
"date": "10/10",
"desc": "test1"
},
{
"date": "09/09",
"desc": "test3"
}
],
"data": [
{
"name": "123",
"datatext": "1234"
}
]
} {
"name": "!##",
"infotext": "!##$"
}
]
}
So how to fix it?

Why am I getting TypeError on code that worked previously?

I have this code to iterate through a json file. The user specifies tiers to be extracted, the names of which are then saved in inputLabels, and this for loop extracts the data from those tiers:
with open(inputfilename, 'r', encoding='utf8', newline='\r\n') as f:
data = json.load(f)
for line in data:
if line['label'] in inputLabels:
elements = [(e['body']['value']).replace(" ", "_") + "\t" for e in line['first']['items']]
outputData.append(elements)
I wrote this code a year ago and have run it multiple times since then with no issues, but running it today I received a TypeError.
if line['label'] in inputLabels:
TypeError: string indices must be integers
I don't understand why my code was able to work before if this is a true TypeError. Why is this only a problem in the code now, and how can I fix it?
EDIT: Pasted part of the json:
{
"contains": [
{
"total": 118,
"generated": "ELAN Multimedia Annotator 6.2",
"id": "xxx",
"label": "BAR001_TEXT",
"type": "AnnotationCollection",
"#context": "http://www.w3.org/ns/ldp.jsonld",
"first": {
"startIndex": "0",
"id": "xxx",
"type": "AnnotationPage",
"items": [
{
"id": "xxx",
"type": "Annotation",
"body": {
"purpose": "transcribing",
"format": "text/plain",
"language": "",
"type": "TextualBody",
"value": ""
},
"#context": "http://www.w3.org/ns/anno.jsonld",
"target": {
"format": "audio/x-wav",
"id": "xxx",
"type": "Audio"
}
},
{
"id": "xxx",
"type": "Annotation",
"body": {
"purpose": "transcribing",
"format": "text/plain",
"language": "",
"type": "TextualBody",
"value": "Dobar vam"
},
"#context": "http://www.w3.org/ns/anno.jsonld",
"target": {
"format": "audio/x-wav",
"id": "xxx",
"type": "Audio"
}
},
{
"id": "xxx",
"type": "Annotation",
"body": {
"purpose": "transcribing",
"format": "text/plain",
"language": "",
"type": "TextualBody",
"value": "Je"
},
"#context": "http://www.w3.org/ns/anno.jsonld",
"target": {
"format": "audio/x-wav",
"id": "xxx",
"type": "Audio"
}
},
Your code would probably work if you replaced for line in data: with for line in data['contains']
Maybe the JSON schema didn't have the "contains" level previously.
A pretty pythonic approach would be using exceptions:
with open(inputfilename, 'r', encoding='utf8', newline='\r\n') as f:
data = json.load(f)
for line in data:
try:
if line['label'] in inputLabels:
elements = [(e['body']['value']).replace(" ", "_") + "\t" for e in line['first']['items']]
outputData.append(elements)
except Exception as e:
print( f"{type(e)} : {e} when trying to use {line}")
Your code will run through and give you a hint about what failed
Turns out it was a pretty simple fix. All of the JSON file was in a container (look at the portion I posted in the question, it's the second line, "contains":). I was able to just remove that container and its open/closing brackets and the code ran successfully after that. Thanks all for your help.

json array into json array - Python

I have the code which worked well until the payload changed.
field_types = [
('subject', str),
('type', str)
]
#reading a raw csv file
output = []
with open('file.csv','r',encoding = 'utf-8-sig') as f:
for row in csv.DictReader(f):
row.update((key, conversion(row[key]))
for key, conversion in field_types)
output.append(row)
with open('file.json','w') as outfile: #storing records as json
json.dump(output,outfile,sort_keys = True, indent = 4)
The outcome was saved nicely:
{
"subject": "1",
"type": "2"
},
{
"subject": "1",
"type": "3"
}
]
The current requirement that it should be saved as i.e. I guess as a jsonarray within an array. Have you had this situation? How to achieve it?
{
"subject":
{
"id": "1"
},
"type": "2"
},
{
"subject":
{
"id": "1"
},
"type": "3"
}
]
This should do the job:
def str2dict(s):
return dict(id=s)
field_types = [
('subject', str2dict),
('type', str)
]

Flatten nested json to csv with nested column names

I have rather very weird requirement now. I have below json and somehow I have to convert it into flat csv.
[
{
"authorizationQualifier": "SDA",
"authorizationInformation": " ",
"securityQualifier": "ASD",
"securityInformation": " ",
"senderQualifier": "ASDAD",
"senderId": "FADA ",
"receiverQualifier": "ADSAS",
"receiverId": "ADAD ",
"date": "140101",
"time": "0730",
"standardsId": null,
"version": "00501",
"interchangeControlNumber": "123456789",
"acknowledgmentRequested": "0",
"testIndicator": "T",
"functionalGroups": [
{
"functionalIdentifierCode": "ADSAD",
"applicationSenderCode": "ASDAD",
"applicationReceiverCode": "ADSADS",
"date": "20140101",
"time": "07294900",
"groupControlNumber": "123456789",
"responsibleAgencyCode": "X",
"version": "005010X221A1",
"transactions": [
{
"name": "ASDADAD",
"transactionSetIdentifierCode": "adADS",
"transactionSetControlNumber": "123456789",
"implementationConventionReference": null,
"segments": [
{
"BPR03": "ad",
"BPR14": "QWQWDQ",
"BPR02": "1.57",
"BPR13": "23223",
"BPR01": "sad",
"BPR12": "56",
"BPR10": "32424",
"BPR09": "12313",
"BPR08": "DA",
"BPR07": "123456789",
"BPR06": "12313",
"BPR05": "ASDADSAD",
"BPR16": "21313",
"BPR04": "SDADSAS",
"BPR15": "11212",
"id": "aDSASD"
},
{
"TRN02": "2424",
"TRN03": "35435345",
"TRN01": "3435345",
"id": "FSDF"
},
{
"REF02": "fdsffs",
"REF01": "sfsfs",
"id": "fsfdsfd"
},
{
"DTM02": "2432424",
"id": "sfsfd",
"DTM01": "234243"
}
],
"loops": [
{
"id": "24324234234",
"segments": [
{
"N101": "sfsfsdf",
"N102": "sfsf",
"id": "dgfdgf"
},
{
"N301": "sfdssfdsfsf",
"N302": "effdssf",
"id": "fdssf"
},
{
"N401": "sdffssf",
"id": "sfds",
"N402": "sfdsf",
"N403": "23424"
},
{
"PER06": "Wsfsfdsfsf",
"PER05": "sfsf",
"PER04": "23424",
"PER03": "fdfbvcb",
"PER02": "Pedsdsf",
"PER01": "sfsfsf",
"id": "fdsdf"
}
]
},
{
"id": "2342",
"segments": [
{
"N101": "sdfsfds",
"N102": "vcbvcb",
"N103": "dsfsdfs",
"N104": "343443",
"id": "fdgfdg"
},
{
"N401": "dfsgdfg",
"id": "dfgdgdf",
"N402": "dgdgdg",
"N403": "234244"
},
{
"REF02": "23423342",
"REF01": "fsdfs",
"id": "sfdsfds"
}
]
}
]
}
]
}
]
}
]
The column header name corresponding to deeper key-value make take nested form, like functionalGroups[0].transactions[0].segments[0].BPR15.
I am able to do this in java using this github project (here you can find the output format I desire in the explanation) in one line:
flatJson = JSONFlattener.parseJson(new File("files/simple.json"), "UTF-8");
The output was:
date,securityQualifier,testIndicator,functionalGroups[1].functionalIdentifierCode,functionalGroups[1].date,functionalGroups[1].applicationReceiverCode, ...
140101,00,T,HP,20140101,ETIN,...
But I want to do this in python. I tried as suggested in this answer:
with open('data.json') as data_file:
data = json.load(data_file)
df = json_normalize(data, record_prefix=True)
with open('temp2.csv', "w", newline='\n') as csv_file:
csv_file.write(df.to_csv())
However, for column functionalGroups, it dumps json as a cell value.
I also tried as suggested in this answer:
with open('data.json') as f: # this ensures opening and closing file
a = json.loads(f.read())
df = pandas.DataFrame(a)
print(df.transpose())
But this also seem to do the same:
0
acknowledgmentRequested 0
authorizationInformation
authorizationQualifier SDA
date 140101
functionalGroups [{'functionalIdentifierCode': 'ADSAD', 'applic...
interchangeControlNumber 123456789
receiverId ADAD
receiverQualifier ADSAS
securityInformation
securityQualifier ASD
senderId FADA
senderQualifier ASDAD
standardsId None
testIndicator T
time 0730
version 00501
Is it possible to do what I desire in python?

Parsing JSON in python for second object

I have a sample JSON in this format:
JSON FILE:
{
"Name": "ABC",
"Phone":"123",
"Address":[{"City":"City-1"},{"Country":"Country-1"}]
}
{
"Name": "ABC-1",
"Phone":"123-1",
"Address":[{"City":"City-2"},{"Country":"Country-2"}]
}
Is there any approach to parse the JSON and loop through the file and print each key-value pair.
The approach I used was through using
json_open = open(json_file)
json_data = json.load(json_open)
print(json_data[Name]) ##should give ABC
print(json_data[Name]) ##should give ABC-1 - unsure about the syntax and format
But I'm currently able to print only the first object values - i.e. name=ABC and not name=ABC-1
There is error in your json file. I modified your json and written code for traverse each element in it.
Error:
Error: Parse error on line 9:
... "Country-1" }]}{ "Name": "ABC-1",
-------------------^
Expecting 'EOF', '}', ',', ']', got '{'
sample.json
{
"data": [
{
"Name": "ABC",
"Phone": "123",
"Address": [
{
"City": "City-1"
},
{
"Country": "Country-1"
}
]
},
{
"Name": "ABC-1",
"Phone": "123-1",
"Address": [
{
"City": "City-2"
},
{
"Country": "Country-2"
}
]
}
]
}
sample.py
import json
json_file='sample.json'
with open(json_file, 'r') as json_data:
data = json.load(json_data)
jin=data['data']
for emp in jin:
print ("Name :"+emp["Name"])
print ("Phone :"+emp["Phone"])
print ("City :"+emp["Address"][0]["City"])
print ("Country :"+emp["Address"][1]["Country"])
Each record on json file is separated by ','. So, your file should look like:
[{
"Name": "ABC",
"Phone": "123",
"Address": [{
"City": "City-1"
}, {
"Country": "Country-1"
}]
},
{
"Name": "ABC-1",
"Phone": "123-1",
"Address": [{
"City": "City-2"
}, {
"Country": "Country-2"
}]
}
]
You can read the file and output as follows :
import json
my_file='test.json'
with open(my_file, 'r') as my_data:
data = json.load(my_data)
print data
for elm in data:
print elm['Phone']
print elm['Name']
print elm['Address'][0]['City']
print elm['Address'][1]['Country']
First of all this is not valid json format,
You can check here you json format
You should use this try.json file
{
"data":[{
"Name": "ABC",
"Phone":"123",
"Address":[{"City":"City-1"},{"Country":"Country-1"}]
}, {
"Name": "ABC-1",
"Phone":"123-1",
"Address":[{"City":"City-2"},{"Country":"Country-2"}]
}]
}
here is try.py
import json
json_open = open("try.json")
json_data = json.load(json_open)
for i in range(len(json_data['data'])):
print(json_data['data'][i]["Name"])
Note : python code is written in python3
output is look line this
ABC
ABC-1

Categories