|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | +#include <jansson.h> |
| 5 | + |
| 6 | +// Update JSON keys in the destination JSON file with keys from the source JSON file |
| 7 | +// This code is chatgpt generated. |
| 8 | +void update_json_keys(const char *source_file, const char *dest_file, const char *output_file) { |
| 9 | + // Load source JSON file |
| 10 | + FILE *source_fp = fopen(source_file, "r"); |
| 11 | + if (!source_fp) { |
| 12 | + perror("Error opening source file"); |
| 13 | + exit(EXIT_FAILURE); |
| 14 | + } |
| 15 | + |
| 16 | + fseek(source_fp, 0, SEEK_END); |
| 17 | + long source_size = ftell(source_fp); |
| 18 | + fseek(source_fp, 0, SEEK_SET); |
| 19 | + |
| 20 | + char *source_content = (char *)malloc(source_size + 1); |
| 21 | + if (!source_content) { |
| 22 | + perror("Error allocating memory for source content"); |
| 23 | + exit(EXIT_FAILURE); |
| 24 | + } |
| 25 | + |
| 26 | + fread(source_content, 1, source_size, source_fp); |
| 27 | + source_content[source_size] = '\0'; // Null-terminate the string |
| 28 | + fclose(source_fp); |
| 29 | + |
| 30 | + json_error_t error; |
| 31 | + json_t *source_json = json_loads(source_content, 0, &error); |
| 32 | + if (!source_json) { |
| 33 | + fprintf(stderr, "Error parsing source JSON: %s\n", error.text); |
| 34 | + exit(EXIT_FAILURE); |
| 35 | + } |
| 36 | + |
| 37 | + free(source_content); // No longer needed after parsing |
| 38 | + |
| 39 | + // Load destination JSON file |
| 40 | + FILE *dest_fp = fopen(dest_file, "r"); |
| 41 | + if (!dest_fp) { |
| 42 | + perror("Error opening destination file"); |
| 43 | + exit(EXIT_FAILURE); |
| 44 | + } |
| 45 | + |
| 46 | + fseek(dest_fp, 0, SEEK_END); |
| 47 | + long dest_size = ftell(dest_fp); |
| 48 | + fseek(dest_fp, 0, SEEK_SET); |
| 49 | + |
| 50 | + char *dest_content = (char *)malloc(dest_size + 1); |
| 51 | + if (!dest_content) { |
| 52 | + perror("Error allocating memory for destination content"); |
| 53 | + exit(EXIT_FAILURE); |
| 54 | + } |
| 55 | + |
| 56 | + fread(dest_content, 1, dest_size, dest_fp); |
| 57 | + dest_content[dest_size] = '\0'; // Null-terminate the string |
| 58 | + fclose(dest_fp); |
| 59 | + |
| 60 | + json_t *dest_json = json_loads(dest_content, 0, &error); |
| 61 | + if (!dest_json) { |
| 62 | + fprintf(stderr, "Error parsing destination JSON: %s\n", error.text); |
| 63 | + exit(EXIT_FAILURE); |
| 64 | + } |
| 65 | + |
| 66 | + free(dest_content); // No longer needed after parsing |
| 67 | + |
| 68 | + // Update destination JSON with source JSON keys and values |
| 69 | + const char *key; |
| 70 | + json_t *value; |
| 71 | + json_object_foreach(source_json, key, value) { |
| 72 | + json_object_set(dest_json, key, value); // Replace or insert keys |
| 73 | + } |
| 74 | + |
| 75 | + // Write updated JSON to output file |
| 76 | + FILE *output_fp = fopen(output_file, "w"); |
| 77 | + if (!output_fp) { |
| 78 | + perror("Error opening output file"); |
| 79 | + exit(EXIT_FAILURE); |
| 80 | + } |
| 81 | + |
| 82 | + if (json_dumpf(dest_json, output_fp, JSON_INDENT(4)) < 0) { |
| 83 | + fprintf(stderr, "Error writing to output JSON file\n"); |
| 84 | + exit(EXIT_FAILURE); |
| 85 | + } |
| 86 | + |
| 87 | + fclose(output_fp); |
| 88 | + json_decref(source_json); |
| 89 | + json_decref(dest_json); |
| 90 | + |
| 91 | + printf("Updated JSON saved to %s\n", output_file); |
| 92 | +} |
| 93 | + |
| 94 | +int main(int argc, char *argv[]) { |
| 95 | + if (argc != 4) { |
| 96 | + fprintf(stderr, "Usage: %s <source_json> <destination_json> <output_json>\n", argv[0]); |
| 97 | + return EXIT_FAILURE; |
| 98 | + } |
| 99 | + |
| 100 | + update_json_keys(argv[1], argv[2], argv[3]); |
| 101 | + return EXIT_SUCCESS; |
| 102 | +} |
0 commit comments