JSON schema
v1: Original form
v2: Redesigned form
Answer mapping · v1
The Spring Boot controller copies the stable answer keys into these columns. Renaming a question doesn't change its answer key.
| Question name | Answer key (valueName) | Database column |
|---|
JSON definition · shared/definitions/relational-storage.v1.json
View on GitHub
{
"title": "Insurance claim",
"elements": [
{
"type": "text",
"name": "q_email",
"valueName": "customer_email",
"dbColumn": "claims.customer_email",
"title": "Email",
"inputType": "email",
"isRequired": true
},
{
"type": "text",
"name": "q_amount",
"valueName": "amount",
"dbColumn": "claims.amount",
"title": "Amount claimed",
"inputType": "number",
"min": 0
},
{
"type": "dropdown",
"name": "incident",
"title": "What happened?",
"choices": [
{
"value": "collision",
"text": "Collision"
},
{
"value": "theft",
"text": "Theft"
},
{
"value": "weather",
"text": "Storm or flood"
}
]
},
{
"type": "comment",
"name": "details",
"title": "Details"
},
{
"type": "boolean",
"name": "priority_handling",
"title": "Priority handling"
}
]
}
JSON definition · shared/definitions/relational-storage.v2.json
View on GitHub
{
"title": "Insurance claim",
"elements": [
{
"type": "panel",
"name": "contact",
"title": "Contact",
"elements": [
{
"type": "text",
"name": "contact_email",
"valueName": "customer_email",
"dbColumn": "claims.customer_email",
"title": "Where should we write to you?",
"inputType": "email",
"isRequired": true
}
]
},
{
"type": "panel",
"name": "claim",
"title": "Claim",
"elements": [
{
"type": "dropdown",
"name": "incident",
"title": "What happened?",
"choices": [
{
"value": "collision",
"text": "Collision"
},
{
"value": "theft",
"text": "Theft"
},
{
"value": "weather",
"text": "Storm or flood"
}
]
},
{
"type": "text",
"name": "claimed_total",
"valueName": "amount",
"dbColumn": "claims.amount",
"title": "Total amount you are claiming",
"inputType": "number",
"min": 0
},
{
"type": "comment",
"name": "details",
"title": "Details"
},
{
"type": "boolean",
"name": "priority_handling",
"title": "Priority handling"
}
]
}
]
}
Client · shared/client/relational-storage.js · sjs:relational-storage.client
View on GitHub
import { Model, Serializer } from "survey-core";
// Optional metadata describing the columns used by the server
Serializer.addProperty("question", { name: "dbColumn", category: "data" });
// Keep answer keys stable when question names change
const survey = new Model(definitions[definitionVersion]);
survey.completeText = "Submit claim";
Client · shared/client/relational-storage.js · sjs:relational-storage.save
View on GitHub
async function saveClaim(data, definitionVersion) {
const response = await fetch("/api/claims", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ data, definitionVersion })
});
if (!response.ok) throw new Error("Save failed");
return response.json();
}
Server · src/main/java/io/surveyjs/examples/RelationalStorageController.java · sjs:relational-storage.server
View on GitHub
// POST /api/claims — original JSON first, then the columns you query
@PostMapping("/api/claims")
@ResponseStatus(HttpStatus.CREATED)
@Transactional
public Map<String, Integer> saveClaim(@RequestBody JsonNode body) {
// A JsonNode, not a DTO: the stored answers keep every key, {} and 1.0 as they arrived
if (!body.hasNonNull("data") || !body.hasNonNull("definitionVersion")) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Expected { data, definitionVersion }");
}
JsonNode data = body.get("data");
int id = storedJson.insertResponse("claim", data, StoredJson.text(body.get("definitionVersion"))); // the original and its version
jdbc.sql("INSERT INTO claims (response_id, customer_email, amount) VALUES (?, ?, ?)") // keys come from valueName
.params(id, StoredJson.text(data.get("customer_email")), number(data.get("amount"))).update();
return Map.of("id", id);
}