{
"band_class": "n77",
"capacity": true,
"connected_bsp": "BSP-2",
"connected_enb": "123",
"connected_gnb": "012",
"device_model": "TITAN",
"enb_locan": {
"coordinates": [
-00011,
00.87
],
"crs": {
"properties": {
"name": "EPSG:4326"
},
"type": "name"
},
"type": "Point"
},
"enb_order_distance": 25,
"trans_dt": "2024-01-11"
}
I need to remove the indentation in the JSON record.
I tried
class PyStreamCallback(StreamCallback):
def __init__(self):
pass
def process(self, inputStream, outputStream):
text = IOUtils.readLines(inputStream, StandardCharsets.UTF_8)
for msg in text:
json_data = json.loads(msg)
string_data = json.dumps(json_data).encode('utf-8')
outputStream.write(bytearray(string_data+'n'))
flowFile = session.get()
if (flowFile != None):
flowFile = session.write(flowFile,PyStreamCallback())
flowFile = session.putAttribute(flowFile, "filename", flowFile.getAttribute('filename').split('.')[0]+'_translated.json')
session.transfer(flowFile, REL_SUCCESS)
I tried with json loads as recommended, Name error gloabl name : data list is not defined
flowFile.getAttribute(‘filename’).split(‘.’)[0]+’_translated.json’)
session.transfer(flowFile, REL_SUCCESS)
2
Answers
It sounds like you have an existing JSON object (i.e., a Python
str
value containing the object). You need to decode that first usingjson.load
, then usejson.dumps
to reserialize the object without extraneous whitespace.For example,
Set
indent
to the required indent (eg0
);Will output: