[PYTHON] Convert (compress) formatted JSON string to 1-line JSON

I checked the method when converting (compressing) JSON character string to 1 line JSON, so make a note.

Method

The following two methods will be introduced.

Assuming the following JSON character string described in the file.

file.py


{
    "Records": [
        {
            "database": {
                "NewImage": {
                    "eventId": {
                        "S": "a4207f7a-5f04-471b-a338-1175182eaa6d"
                    },
                    "isCompleted": {
                        "BOOL": True
                    }
                },
                "OldImage": {
                    "eventId": {
                        "S": "a4207f7a-5f04-471b-a338-1175182eaa6d"
                    },
                    "isCompleted": {
                        "BOOL": False
                    }
                }
            }
        }
    ]
}

Method 1: Use Python

$ python
>>> import json
>>> f = open("file.json")
>>> json.dumps(f.read()).replace("\\n","").replace("\\","").replace("    ", "")
'"{"Records": [{"database": {"NewImage": {"eventId": {"S": "a4207f7a-5f04-471b-a338-1175182eaa6d"},"isCompleted": {"BOOL": True}},"OldImage": {"eventId": {"S": "a4207f7a-5f04-471b-a338-1175182eaa6d"},"isCompleted": {"BOOL": False}}}}]}"'

In .replace ("", "") , the indent (space`` 4) is replaced and deleted. If a different character string is indented in the JSON character string, the replacement source of this part should be changed.

Method 2: Use the jq command

Basically, cat <filename> | jq -c can be used.

However, jq cannot handle True and False as boolean, so if you try to execute it as it is for this file.json, the following error will occur.

$ cat file.json | jq -c
parse error: Invalid numeric literal at line 11, column 0

Therefore, it is necessary to change True to true and False to false in advance. If file.json is modified in advance, it can be executed as follows.

$ cat file.json | jq -c
{"Records":[{"database":{"NewImage":{"eventId":{"S":"a4207f7a-5f04-471b-a338-1175182eaa6d"},"isCompleted":{"BOOL":true}},"OldImage":{"eventId":{"S":"a4207f7a-5f04-471b-a338-1175182eaa6d"},"isCompleted":{"BOOL":false}}}}]}

that's all

Recommended Posts

Convert (compress) formatted JSON string to 1-line JSON
Convert json to excel
Convert hexadecimal string to binary
[python] Convert date to string
Convert Tweepy Status object to JSON
Convert a string to an image
Convert Excel data to JSON with python
Convert array (struct) to json with golang
Convert to HSV
Convert XLSX to CSV on the command line
Convert json format data to txt (using yolo)
Convert pandas dataframe elements to regular string type
Parse a JSON string written to a file in Python
I convert AWS JSON data to CSV like this
How to convert JSON file to CSV file with Python Pandas
Convert / return class object to JSON format in Python
How to convert Json file to CSV format or EXCEL format
Convert Pascal VOC format xml file to COCO format json file
Convert 202003 to 2020-03 with pandas
Convert kanji to kana
Convert jupyter to py
Convert keras-yolo3 to onnx
How to read JSON
Convert dict to array
Convert Select query obtained from Postgre with Go to JSON
I want to convert an ISO-8601 character string to Japan time
Read json in C # and convert to dictionary type (forced)