Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
JSON (JavaScript Object Notation) is a lightweight, text-based, language-independent data exchange format that is easy for machines and humans to write and read. JSON can represent two structured types: arrays and objects. An object is an unordered collection of zero or more values/name pairs. An array is an ordered sequence of zero or more values. The values can be numbers, strings, booleans, null, and these two structured types.
We can use the Jackson JSON library in Java to convert objects to and from JSON. Actions work with the JsonNode type, and the framework gives utility methods for conversion in the play.libs.Json API.
JSON AND JavaScript
Mapping Java objects to JSON
Jackson helps you to easily convert Java objects to JSON by looking at getters, setters, and field names. As an, e.g., we will use the following simple Java object:
// Note: can use getters/setters as well; here we just use public fields directly.
// if using getters/setters, you can keep the fields `protected` or `private`
public static class Person {
public String firstName;
public String lastName;
public int age;
}
You can also try this code with Online Java Compiler
We can parse the JSON representation of the object to create a new Person:
Person person = new Person();
person.firstName = "Foo";
person.lastName = "Bar";
person.age = 30;
JsonNode personJson = Json.toJson(person); // {"firstName": "Foo", "lastName": "Bar", "age": 30}
Similarly, we can write the Person object to a JsonNode:
// parse the JSON as a JsonNode
JsonNode json = Json.parse("{\"firstName\":\"Foo\", \"lastName\":\"Bar\", \"age\":13}");
// read the JsonNode as a Person
Person person = Json.fromJson(json, Person.class);
You can also try this code with Online Java Compiler
A JSON request is an HTTP request with the help of a valid JSON payload as request body. Its Content-Type header must specify the json/text or application/json MIME type.
By default, an action uses any content body parser, which you can utilize to get the body as JSON :
public Result sayHello(Http.Request request) {
JsonNode json = request.body().asJson();
if (json == null) {
return badRequest("Expecting Json data");
} else {
String name = json.findPath("name").textValue();
if (name == null) {
return badRequest("Missing parameter [name]");
} else {
return ok("Hello " + name);
}
}
}
public Result sayHello(Http.Request request) {
Optional<Person> person = request.body().parseJson(Person.class);
return person.map(p -> ok("Hello, " + p.firstName)).orElse(badRequest("Expecting Json data"));
}
You can also try this code with Online Java Compiler
JSON (JavaScript Object Notation) is a text-based, lightweight, language-independent data exchange format that is easy for machines and humans to read and write. JSON can represent two structured types: arrays and objects.
How is JSON different from JavaScript?
The last significant difference between JSON and JavaScript objects is their presentation. JSON is presented in a string. These are referred to as JSON strings. JavaScript objects can contain strings, but they are, as their name suggests, objects instead of strings.
Why do we need JSON?
The JSON format transmits and serializes structured data over network connections. It is primarily utilized to transmit data between a server and web applications. Web services and APIs use JSON format to provide public data. We can use it with modern programming languages.
Conclusion
I hope this article gave you insights into working with JSON for java developers.
We hope this blog has helped you increase your knowledge regarding AWS Step functions, and if you liked this blog, check other links. Do upvote our blog to help other ninjas grow. Happy Coding!"