如何检测双引号内是否有空格(使用jq或在linux中)?

问题描述 投票:0回答:2

示例json数据如下: 一个文件中有多行。几行带有 NOK 样本和其余内容都可以。 如何识别这些有空格的有问题的行?

诺克:

[[{"PK":"ABCD||1000000","id":"ABCD||1000000","chargingRules":{"code":[{"actions":[{"attributeInfo":{"name":"Rate","resultContext":"RATING"},"parameters":[{"name":"Target","value":{"data":{"type":0,"value":"ABCD_BUCKET"}}},{"name":"Rate","value":{"data":{"type":0,"value":"VR_0_50"}}}]},{"attributeInfo":{"name":"Tax-Selection","resultContext":"RATING"},"parameters":[{"name":"Data","value":{"data":{"type":0,"value":"Tax"}}}]}],"condContainer":{"conditions":[{"criteria":{"name":"Get-Variable","sourceContext":"VARIABLES"},"criteriaArgs":[{"name":"Name","value":{"type":0,"value":"GVCallType"}}],"operator":0,"value":{"type":3,"value":"CallType.IMS"}},{"criteria":{"name":"Get-Variable","sourceContext":"VARIABLES"},"criteriaArgs":[{"name":"Name","value":{"type":0,"value":"GVDest_Country"}}],"operator":0,"value":{"type":0,"value":"IND"}}],"operator":0},"modifiedDate":1725306076363,"rulename":"VOICE","schemaVersion":0},{"actions":[{"attributeInfo":{"name":"Rate","resultContext":"RATING"},"parameters":[{"name":"Target","value":{"data":{"type":0,"value":**"ABCD_BUCKET "**}}},{"name":"Rate","value":{"data":{"type":0,"value":"SR_0_12"}}}]},{"attributeInfo":{"name":"Tax-Selection","resultContext":"RATING"},"parameters":[{"name":"Data","value":{"data":{"type":0,"value":"Tax"}}}]}],"condContainer":{"conditions":[{"criteria":{"name":"Get-Variable","sourceContext":"VARIABLES"},"criteriaArgs":[{"name":"Name","value":{"type":0,"value":"GVCallType"}}],"operator":0,"value":{"type":3,"value":"CallType.SMS"}},{"criteria":{"name":"Get-Variable","sourceContext":"VARIABLES"},"criteriaArgs":[{"name":"Name","value":{"type":0,"value":"GVDest_Country"}}],"operator":0,"value":{"type":0,"value":"IND"}}],"operator":0},"modifiedDate":1725306076363,"rulename":"SMS","schemaVersion":0},{"actions":[{"attributeInfo":{"name":"Rate","resultContext":"RATING"},"parameters":[{"name":"Rate","value":{"data":{"type":0,"value":"DR_0_08"}}},{"name":"Target","value":{"data":{"type":0,"value":**"ABCD_BUCKET "**}}}]},{"attributeInfo":{"name":"Tax-Selection","resultContext":"RATING"},"parameters":[{"name":"Data","value":{"data":{"type":0,"value":"Tax"}}}]}],"condContainer":{"conditions":[{"criteria":{"name":"Get-Variable","sourceContext":"VARIABLES"},"criteriaArgs":[{"name":"Name","value":{"type":0,"value":"GVCallType"}}],"operator":0,"value":{"type":3,"value":"CallType.PS"}}],"operator":0},"modifiedDate":1725306076363,"rulename":"DATA","schemaVersion":0}],"codeMode":1,"items":[{"componentId":"SR_0_12","isPrimary":1,"isRoot":0,"overrideInst":0,"subType":6,"type":1},{"componentId":"VR_0_50","isPrimary":1,"isRoot":0,"overrideInst":0,"subType":6,"type":1},{"componentId":"ABCD_BUCKET","isPrimary":1,"isRoot":0,"overrideInst":0,"subType":2,"type":1},{"componentId":"DR_0_08","isPrimary":1,"isRoot":0,"overrideInst":0,"subType":6,"type":1}],"splitChFlag":0,"tariffName":"ChargingRules"}}],[{"Status":0}]]

好的:

[[{"PK":"PQRS||2000000","id":"PQRS||2000000","chargingRules":{"code":[{"actions":[{"attributeInfo":{"name":"Rate","resultContext":"RATING"},"parameters":[{"name":"Target","value":{"data":{"type":0,"value":"ABCD_BUCKET"}}},{"name":"Rate","value":{"data":{"type":0,"value":"VR_0_50"}}}]},{"attributeInfo":{"name":"Tax-Selection","resultContext":"RATING"},"parameters":[{"name":"Data","value":{"data":{"type":0,"value":"Tax"}}}]}],"condContainer":{"conditions":[{"criteria":{"name":"Get-Variable","sourceContext":"VARIABLES"},"criteriaArgs":[{"name":"Name","value":{"type":0,"value":"GVCallType"}}],"operator":0,"value":{"type":3,"value":"CallType.IMS"}},{"criteria":{"name":"Get-Variable","sourceContext":"VARIABLES"},"criteriaArgs":[{"name":"Name","value":{"type":0,"value":"GVDest_Country"}}],"operator":0,"value":{"type":0,"value":"IND"}}],"operator":0},"modifiedDate":1725306076363,"rulename":"VOICE","schemaVersion":0},{"actions":[{"attributeInfo":{"name":"Rate","resultContext":"RATING"},"parameters":[{"name":"Target","value":{"data":{"type":0,"value":"ABCD_BUCKET"}}},{"name":"Rate","value":{"data":{"type":0,"value":"SR_0_12"}}}]},{"attributeInfo":{"name":"Tax-Selection","resultContext":"RATING"},"parameters":[{"name":"Data","value":{"data":{"type":0,"value":"Tax"}}}]}],"condContainer":{"conditions":[{"criteria":{"name":"Get-Variable","sourceContext":"VARIABLES"},"criteriaArgs":[{"name":"Name","value":{"type":0,"value":"GVCallType"}}],"operator":0,"value":{"type":3,"value":"CallType.SMS"}},{"criteria":{"name":"Get-Variable","sourceContext":"VARIABLES"},"criteriaArgs":[{"name":"Name","value":{"type":0,"value":"GVDest_Country"}}],"operator":0,"value":{"type":0,"value":"IND"}}],"operator":0},"modifiedDate":1725306076363,"rulename":"SMS","schemaVersion":0},{"actions":[{"attributeInfo":{"name":"Rate","resultContext":"RATING"},"parameters":[{"name":"Rate","value":{"data":{"type":0,"value":"DR_0_08"}}},{"name":"Target","value":{"data":{"type":0,"value":"ABCD_BUCKET"}}}]},{"attributeInfo":{"name":"Tax-Selection","resultContext":"RATING"},"parameters":[{"name":"Data","value":{"data":{"type":0,"value":"Tax"}}}]}],"condContainer":{"conditions":[{"criteria":{"name":"Get-Variable","sourceContext":"VARIABLES"},"criteriaArgs":[{"name":"Name","value":{"type":0,"value":"GVCallType"}}],"operator":0,"value":{"type":3,"value":"CallType.PS"}}],"operator":0},"modifiedDate":1725306076363,"rulename":"DATA","schemaVersion":0}],"codeMode":1,"items":[{"componentId":"SR_0_12","isPrimary":1,"isRoot":0,"overrideInst":0,"subType":6,"type":1},{"componentId":"VR_0_50","isPrimary":1,"isRoot":0,"overrideInst":0,"subType":6,"type":1},{"componentId":"ABCD_BUCKET","isPrimary":1,"isRoot":0,"overrideInst":0,"subType":2,"type":1},{"componentId":"DR_0_08","isPrimary":1,"isRoot":0,"overrideInst":0,"subType":6,"type":1}],"splitChFlag":0,"tariffName":"ChargingRules"}}],[{"Status":0}]]

差异:

..."parameters":[{"name":"Target","value":{"data":{"type":0,"value":"ABCD_BUCKET "}}},...
..."parameters":[{"name":"Target","value":{"data":{"type":0,"value":"ABCD_BUCKET"}}},...
linux bash jq
2个回答
0
投票

您只需使用一个简单的 bash 脚本即可。

喜欢:

if grep -q '[* ]' file.txt; then
    echo NOK
else
    echo OK
fi

0
投票

您可以在 python 中使用此代码,例如:

import json

# Function to check if a string contains leading or trailing spaces
def has_unwanted_spaces(value):
    return isinstance(value, str) and (value != value.strip())

# Function to recursively check all values in a JSON object
def check_json_for_spaces(data, problematic_lines, line):
    if isinstance(data, dict):
        for key, value in data.items():
            check_json_for_spaces(value, problematic_lines, line)
    elif isinstance(data, list):
        for item in data:
            check_json_for_spaces(item, problematic_lines, line)
    elif has_unwanted_spaces(data):
        problematic_lines.append(line)
        return

# Read the file and check each line
def check_file_for_problematic_lines(file_path):
    problematic_lines = []
    with open(file_path, 'r') as file:
        for line_number, line in enumerate(file, start=1):
            try:
                # Parse the JSON line
                data = json.loads(line.strip())
                # Check for unwanted spaces in the JSON data
                check_json_for_spaces(data, problematic_lines, line_number)
            except json.JSONDecodeError:
                print(f"Error parsing line {line_number}: {line.strip()}")

    # Return the problematic lines
    return problematic_lines

# Path to your JSON file
file_path = 'data.json'

# Get the problematic lines
problematic_lines = check_file_for_problematic_lines(file_path)

# Output the problematic lines
if problematic_lines:
    print("Problematic lines found:")
    for line_number in problematic_lines:
        print(f"Line {line_number} has unwanted spaces.")
else:
    print("No problematic lines found.")
© www.soinside.com 2019 - 2024. All rights reserved.