skip to Main Content
{
    "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


  1. It sounds like you have an existing JSON object (i.e., a Python str value containing the object). You need to decode that first using json.load, then use json.dumps to reserialize the object without extraneous whitespace.

    For example,

    >>> s = '{n  "a": 1,n  "b": 2n}'
    >>> print(s)
    {
      "a": 1,
      "b": 2
    }
    >>> json.dumps(json.loads(s))
    '{"a": 1, "b": 2}'
    
    Login or Signup to reply.
  2. Set indent to the required indent (eg 0);

    import json
    
    raw = '{"band_class": "n77", "capacity": true, "connected_bsp": "BSP-2", "connected_enb": "123", "connected_gnb": "012", "device_model": "TITAN", "enb_locan": {"coordinates": [1, 2 ], "crs": {"properties": {"name": "EPSG:4326" }, "type": "name" }, "type": "Point" }, "enb_order_distance": 25, "trans_dt": "2024-01-11" }'
    
    j = json.loads(raw);
    
    r = json.dumps(j,indent = 0)
    
    print(r)
    

    Will output:

    {
    "band_class": "n77",
    "capacity": true,
    "connected_bsp": "BSP-2",
    "connected_enb": "123",
    "connected_gnb": "012",
    "device_model": "TITAN",
    "enb_locan": {
    "coordinates": [
    1,
    2
    ],
    "crs": {
    "properties": {
    "name": "EPSG:4326"
    },
    "type": "name"
    },
    "type": "Point"
    },
    "enb_order_distance": 25,
    "trans_dt": "2024-01-11"
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search