question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Root name of JSON data format

See original GitHub issue

This is Person.java

@JsonRootName("Person")
public class Person {
    private String name;
    private double height;
    private int age;
    @JsonSerialize(using=CustomDateSerializer.class)
    private Date date;
    @JsonProperty("Address")
    private Address address;
        //normal constructor
        //getters/setters
}

This is my main class:

public class AppJson {
    public static void main(String[] arg) throws JsonGenerationException,
            JsonMappingException, IOException {
        Address a = new Address("Jln Koli", "90121", "Vila", "Belgium");
        Person p = new Person("Ali Bin Baba", new Date(), 90.0, 12, a);

        Json json = new Json(p);
        System.out.println(json.toString());
    }
}

Output:

{
  "name" : "Ali Bin Baba",
  "age" : 12,
  "date" : "06-12-2014",
  "Address" : {
    "postcode" : "90121",
    "city" : "Vila",
    "state" : "Belgium",
    "street" : "Jln Koli"
  }
}

But, the output I want is like this:

{
  "Person": {
    "name": "Ali Bin Baba",
    "age": "12",
    "date": "06-12-2014",
    "Address": {
      "postcode": "90121",
      "city": "Vila",
      "state": "Belgium",
      "street": "Jln Koli"
    }
  }
}

My dependencies:

<dependencies>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.3.3</version>
  </dependency>
</dependencies>

Did you know, which part is wrong??

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Jun 13, 2014

Two ways to go; either use something like:

@JsonRootName("Person")
public class PersonList extends ArrayList<Person> { }

to specify root name for your List subtype.

Or use method in ObjectWriter to use whatever name you want for object:

ObjectWriter w = ...;
w.withRootName("Person").writeValueAsString(list);
0reactions
cowtowncodercommented, Jun 13, 2014

You are not using “withRootName(“Person”)” in there.

And from now on, let’s use mailing lists for questions, discussions. Issue trackers are for bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON root element - Stack Overflow
The outermost level of a JSON document is either an "object" (curly braces) or an "array" (square brackets). Any software that converts JSON ......
Read more >
An Introduction to JSON | DigitalOcean
JSON — short for JavaScript Object Notation — is a format for sharing data. As its name suggests, JSON is derived from the...
Read more >
Jackson JSON - Using @JsonRootName to customize POJO ...
@JsonRootName annotation is used to indicate name of the POJO that should be serialized. For serialization to work with @JsonRootName ...
Read more >
Add a Root Node to JSON Output with the ROOT Option - SQL ...
Here's another example of a FOR JSON clause with the ROOT option. This example specifies a value for the optional RootName argument.
Read more >
10 Using JSON Documents
6 Mapping Root-Level Collections. If you use the @XmlRootElement(name="root") annotation to specify a root level, the JSON document can be marshaled ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found