如何在AWK中生成JSON对象?自定义实现需求问询
Got it, let's tackle this AWK JSON generation task step by step. I'll cover two scenarios: directly processing your input file, and generating JSON from the pre-built multi-dimensional array you already have.
Solution 1: Process the Input File Directly
This script will skip irrelevant lines, extract shareholder data, map it to your desired JSON schema, handle escaped quotes, and output the JSON objects as requested.
BEGIN { # Define dynamic JSON schema mapping: input field → JSON key schema["imie"] = "Name" schema["nazwisko"] = "Surname" schema["pesel"] = "ID" schema["funkcja"] = "Role" in_shareholders = 0 # Flag to track if we're in the shareholder data block entry_count = 0 # Count of key-value pairs collected for the current entry } # Trigger entry into shareholder data mode /dane wspólników/ { in_shareholders = 1 next } # Exit shareholder data mode when hitting unrelated sections /czwarta linia|reprezentanci/ { in_shareholders = 0 delete current_entry # Clear incomplete data if any entry_count = 0 next } # Process lines while in shareholder data mode in_shareholders { key = $1 # Extract full value (handles cases like Ja"nD with quotes/embedded spaces) value = substr($0, index($0, $2)) # Escape double quotes in values to valid JSON gsub(/"/, "\\\"", value) # Map input key to JSON key if it exists in our schema if (schema[key] != "") { current_entry[schema[key]] = value entry_count++ } # When we have a full set of 4 fields, output the JSON object if (entry_count == 4) { printf "{ " first_field = 1 for (json_key in current_entry) { if (!first_field) printf ", " printf "\"%s\": \"%s\"", json_key, current_entry[json_key] first_field = 0 } printf " } " # Reset for next entry delete current_entry entry_count = 0 } }
How to use:
Run it with your input file like this:
awk -f generate_json.awk your_input_file.txt
Solution 2: Generate JSON from Your Pre-built Multi-dimensional Array
If you already have the cleaned multi-dimensional array, this simpler script will map it to your schema and output the required JSON:
BEGIN { # Define schema order (matches your array's field positions) json_keys[1] = "Name" json_keys[2] = "Surname" json_keys[3] = "ID" json_keys[4] = "Role" # Populate with your existing array data (adjust indices as needed) data[1][1] = "JanA" data[1][2] = "NowakA" data[1][3] = "1234A" data[1][4] = "PrezesA" data[2][1] = "JanD" data[2][2] = "NowakD" data[2][3] = "12341D" data[2][4] = "PrezesD" data[3][1] = "JanC" data[3][2] = "NowakC" data[3][3] = "12342C" data[3][4] = "PrezesC" # Loop through the array to generate JSON for (i = 1; i <= length(data); i++) { printf "{ " first_field = 1 for (j = 1; j <= 4; j++) { if (!first_field) printf ", " # Optional: Remove quotes for numeric IDs if needed # if (data[i][j] ~ /^[0-9]+$/) { # printf "\"%s\": %s", json_keys[j], data[i][j] # } else { printf "\"%s\": \"%s\"", json_keys[j], data[i][j] # } first_field = 0 } printf " } " } printf "\n" }
Optional tweak:
If some IDs are pure numbers and you want them unquoted in JSON, uncomment the numeric check section in the script.
内容的提问来源于stack exchange,提问作者djsowa

