Postman环境下用原生JS实现JSON双向转换的最简方案
Since you're limited to Postman Sandbox and native JS, we can use reduce for the verbose-to-concise conversion and flatMap + Object.entries for the reverse—both are lightweight and require minimal code.
1. Convert Verbose to Concise Format
We'll use Array.reduce() to build the concise object, handling duplicate keys by converting their values into arrays:
function verboseToConcise(verbose) { return verbose.reduce((acc, { Key, Value }) => { // If key doesn't exist, set it to the value if (!acc[Key]) { acc[Key] = Value; } // If key exists and isn't an array, convert to array and add new value else if (!Array.isArray(acc[Key])) { acc[Key] = [acc[Key], Value]; } // If key exists as array, push new value else { acc[Key].push(Value); } return acc; }, {}); }
How It Works:
- We start with an empty object as the accumulator.
- For each item in the verbose array:
- If the key is new, we add it to the accumulator with its value.
- If the key exists but isn't an array, we convert the existing value to an array and append the new value.
- If the key is already an array, we simply push the new value into it.
2. Convert Concise Back to Verbose Format
To reverse the process, we'll use Object.entries() to iterate over the concise object's keys, then flatMap() to generate the verbose array entries (handling arrays by creating multiple instances):
function conciseToVerbose(concise) { return Object.entries(concise).flatMap(([Key, value]) => { // If value is an array, create an entry for each element with incrementing Instance if (Array.isArray(value)) { return value.map((Value, index) => ({ Key, Value, Instance: index + 1 })); } // If value is single, create a single entry with Instance 1 else { return [{ Key, Value: value, Instance: 1 }]; } }); }
How It Works:
Object.entries()turns the concise object into an array of[key, value]pairs.flatMap()processes each pair:- For array values, we map each element to a verbose object with
Instanceset to the element's index + 1 (starting at 1 as in your example). - For single values, we return a single verbose object with
Instance: 1.
- For array values, we map each element to a verbose object with
flatMap()automatically flattens the resulting array of arrays into a single array, matching the original verbose structure.
Example Usage
Let's test with your sample data:
// Verbose input const verbose = [ { "Key": "Name", "Value": "John Doe", "Instance": 1 }, { "Key": "Age", "Value": "33", "Instance": 1 }, { "Key": "Child", "Value": "Jane", "Instance": 1 }, { "Key": "Child", "Value": "Rocky", "Instance": 2 } ]; // Convert to concise const concise = verboseToConcise(verbose); console.log(concise); // Output: { "Name": "John Doe", "Age": "33", "Child": ["Jane", "Rocky"] } // Convert back to verbose const convertedBack = conciseToVerbose(concise); console.log(convertedBack); // Output: Same as original verbose array (order preserved in modern JS environments)
These functions use only native JS methods supported in Postman's sandbox, avoid messy loops, and cleanly handle multi-instance key pairs.
内容的提问来源于stack exchange,提问作者AutomatedChaos

