data
JSON Transformation Prompt
JSON transformations are error-prone when null fields or unexpected types appear. This prompt requires both a sample input and the expected output, so the AI can derive the exact transformation logic rather than guessing. The null-handling section prevents silent data loss when real data differs from the sample.
Prompt Template
You are a data engineer specialising in JSON data transformation.
Write code to transform the following JSON structure.
Input JSON sample:
```json
{{input_json}}
```
Target output structure:
```json
{{output_json}}
```
Language: {{language}}
Transformation requirements: {{requirements}}
Provide:
1. Complete, runnable transformation code with comments
2. A plain-English description of each transformation step
3. How to handle missing or null fields
4. Edge cases the transformation should account for
5. A test to verify the transformation is correctVariables
{{input_json}}A sample of the input JSON structure (5-10 records){{output_json}}The desired output structure for the same sample{{language}}Code language: JavaScript, Python, jq, or SQL (for JSON columns){{requirements}}Additional transformation rules, e.g., "flatten nested objects", "rename keys to snake_case", or "None"Example
Input
input_json:
{"user_id": 123, "full_name": "Alice Smith", "address": {"city": "London", "country": "UK"}}
output_json:
{"id": 123, "name": "Alice Smith", "city": "London", "country": "UK"}
language: JavaScript
requirements: Flatten address fields into the top level, rename user_id to idOutput
function transformUser(input) {
if (!input) return null;
return {
id: input.user_id,
name: input.full_name,
city: input.address?.city ?? null,
country: input.address?.country ?? null,
};
}
// Test
const result = transformUser(input);
console.assert(result.id === 123, 'id mismatch');
console.assert(result.city === 'London', 'city mismatch');Related Tools
FAQ
- Can I use this to transform large nested JSON files?
- Yes. Provide a representative sample (not the full file) and describe the full structure in requirements. The generated code will work on the complete file via streaming parsers like JSONStream in Node.js for very large files.
- Can this generate a jq filter instead of code?
- Yes. Set language to "jq" and the AI will produce a jq expression you can pipe directly: `cat data.json | jq '[.[] | {id: .user_id, name: .full_name, city: .address.city}]'`.
- How do I transform arrays of nested objects?
- Include an array sample (3-5 elements) in input_json and a transformed array in output_json. The AI will generate map/reduce logic to handle the entire array consistently.
Related Prompts
Data Analysis Prompt
Most AI data analysis prompts produce vague observations like "sales increased in Q2". Thi...
CSV Data Processing PromptCSV processing tasks involve numerous small decisions about null handling, column types, a...
API Response Parsing PromptAPI integrations break most often when the response structure changes or contains unexpect...