Compare dynamic XML/JSON content with static tokenised payload and retrieve token values

Multi tool use
Compare dynamic XML/JSON content with static tokenised payload and retrieve token values
I am implementing mock http response server. The server has to validate the input request url and payload then match the request to configured response then return it back to the caller.
In that i need help on validating the http request dynamic content payload with static tokenised payload.
So when i got the request payload say json, compare it with configured tokenised content, and return failure if it not matches.
e.g) I am doing the same for request url with below code.
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriTemplate;
public static void main(String args) {
//template url
String template = "/name/{name}/age/{age}";
UriTemplate uriTemplate = new UriTemplate(template);
//actual url
String uri = "/name/Bob/age/47";
Map<String, String> parameters = new HashMap<>();
//returns Map
System.out.println("Dynamic Content Map: " + uriTemplate.match(uri));
System.out.println("URL Matched: " +uriTemplate.matches(uri));
parameters.put("name", "Foo");
parameters.put("age", "37");
UriComponentsBuilder builder = UriComponentsBuilder.fromPath(template);
System.out.println(builder.buildAndExpand(parameters).toUriString());
}
OUTPUT:
Dynamic Content: {name=Bob, age=47}
URL Matched: true
/name/Foo/age/37
So if you look at this code, UriTemplate is capable of comparing the static content (name/age) configured with dynamic value (Bob/47) filled content.
The same comparison i want to do it in request payload. Now challenges are
I know i can go with XML and JSON parser to compare, but how to compare static with dynamic variables inside the content and retrieve it?
e.g) Static {"name" : "$name", "age" : "$age"}
{"name" : "$name", "age" : "$age"}
e.g) Dynamic {"name" : "Bob", "age" : 47}
{"name" : "Bob", "age" : 47}
Is there any tool i can pass both static and dynamic content, and i will get isMatched and retrieve the dynamic constants in a map like above shown uriTemplate examples?
Give me some hints/ideas on comparing and extracting dynamic fields?
This question has not received enough attention.
@kumesana Thanks! Yes i know the standard parsing of XML and JSON. But want to know how to retrieve tokens from it dynamically if i have some template already?
– Kanagavelu Sugumar
Jul 3 at 6:38
You don't have some template already. Or if you do, throw them away because they won't work and you should never have gone that path. So, the hypothetical is not very useful. Do it the same way everyone else is doing it. It works for the only reason that it's how everyone is already doing it. They call it "standards".
– kumesana
Jul 3 at 6:45
1 Answer
1
XML and JSON are serialized representations of a structure.
The dynamic content that you refer is actually an instance of that structure.
What I think you're looking for is XSD/DTD [1] (define the type of your structure) for XML and json-schema [3] for JSON.
There are multiple strategies here. Depending on the service to be validated. You can convert json to xml and use the same XSD to validate both serialization methods. There are various frameworks to help you achieve this. However, the first step would be to write those schemas (XSD/DTD for XML and/or json-schema for JSON).
Thanks! PlusOne. Yes, i was thinking about it, and checking for better tool, need to check on XSD to work on json?
– Kanagavelu Sugumar
yesterday
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
URLs are simple Strings. XML and JSON are complex constructions. While it may make sense to verify if a URL matches certain String rules in nearly the same way as you'd do with a regex, XML and JSON don't work like that. You will not try to match your XML or JSON to something. You will just have them parsed or bound to a class using JAXB or Jackson or parsers, and extract the information out of the result. In other word, just follow literally any tutorial on how to make webservices with Java. They'll tell you how to received XML/JSON data.
– kumesana
Jul 3 at 6:28