function mirrorJson(jsonData, path = [], prefix = "") {
// Validate input
if (typeof jsonData !== 'object' || jsonData === null) {
console.error("Invalid input: jsonData must be an object.");
return;
}
// Log the current path
console.log(`Mirroring at path: ${path.join('.')}, Prefix: ${prefix}`);
const mirroredData = {};
for (const key in jsonData) {
if (jsonData.hasOwnProperty(key)) {
const currentPath = [...path, key]; // Create a new path array
const value = jsonData[key];
if (typeof value === 'object' && value !== null) {
// Recursive call for nested objects/arrays
mirrorJson(value, currentPath, prefix ? `${prefix}.${key}` : key);
} else {
// Assign the value to the mirrored data
mirroredData[prefix ? `${prefix}.${key}` : key] = value;
}
}
}
return mirroredData;
}
// Example usage (for manual execution)
// const originalJson = {
// "name": "John Doe",
// "age": 30,
// "address": {
// "street": "123 Main St",
// "city": "Anytown"
// },
// "hobbies": ["reading", "hiking"]
// };
// const mirroredJson = mirrorJson(originalJson);
// console.log(JSON.stringify(mirroredJson, null, 2));
Add your comment