JSON root elements and encoding












0















I'm having a JSON deserialized from Java as follow:



Java



jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO);


JSON



  "accounts" : [ {
"name" : "1009427721",
"value" : 16850.79,
"children" : [ {
"name" : "BITCOIN EARNINGS",
"value" : 10734.24,
"children" : [ {
"name" : "2017",
"value" : 1037.82,
"children" : [ {
"name" : "07",
"value" : 518.91
} ]
} ]
}, ...


The deserialized Java POJO being:



public class GraphDTO {

private Set<Account> accounts = new HashSet<>();

public Set<Account> getAccounts() {
return accounts;
}
}


Questions




  1. How can I remove "accounts" from the generated JSON (first line) ?

  2. Injecting the JSON form into JavaScript, I'm getting an encoded form like:
    var data = { "accounts" : [ { ...
    How can I avoid this ?










share|improve this question

























  • How are you parsing JSON in javascript? Can you provide any code?

    – theapologist
    Nov 26 '18 at 13:22











  • Yes: <script> var data = <c:out value="${graphDTOJSON}"/>; ... </script>

    – Hey StackExchange
    Nov 26 '18 at 13:26
















0















I'm having a JSON deserialized from Java as follow:



Java



jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO);


JSON



  "accounts" : [ {
"name" : "1009427721",
"value" : 16850.79,
"children" : [ {
"name" : "BITCOIN EARNINGS",
"value" : 10734.24,
"children" : [ {
"name" : "2017",
"value" : 1037.82,
"children" : [ {
"name" : "07",
"value" : 518.91
} ]
} ]
}, ...


The deserialized Java POJO being:



public class GraphDTO {

private Set<Account> accounts = new HashSet<>();

public Set<Account> getAccounts() {
return accounts;
}
}


Questions




  1. How can I remove "accounts" from the generated JSON (first line) ?

  2. Injecting the JSON form into JavaScript, I'm getting an encoded form like:
    var data = { "accounts" : [ { ...
    How can I avoid this ?










share|improve this question

























  • How are you parsing JSON in javascript? Can you provide any code?

    – theapologist
    Nov 26 '18 at 13:22











  • Yes: <script> var data = <c:out value="${graphDTOJSON}"/>; ... </script>

    – Hey StackExchange
    Nov 26 '18 at 13:26














0












0








0








I'm having a JSON deserialized from Java as follow:



Java



jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO);


JSON



  "accounts" : [ {
"name" : "1009427721",
"value" : 16850.79,
"children" : [ {
"name" : "BITCOIN EARNINGS",
"value" : 10734.24,
"children" : [ {
"name" : "2017",
"value" : 1037.82,
"children" : [ {
"name" : "07",
"value" : 518.91
} ]
} ]
}, ...


The deserialized Java POJO being:



public class GraphDTO {

private Set<Account> accounts = new HashSet<>();

public Set<Account> getAccounts() {
return accounts;
}
}


Questions




  1. How can I remove "accounts" from the generated JSON (first line) ?

  2. Injecting the JSON form into JavaScript, I'm getting an encoded form like:
    var data = { "accounts" : [ { ...
    How can I avoid this ?










share|improve this question
















I'm having a JSON deserialized from Java as follow:



Java



jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO);


JSON



  "accounts" : [ {
"name" : "1009427721",
"value" : 16850.79,
"children" : [ {
"name" : "BITCOIN EARNINGS",
"value" : 10734.24,
"children" : [ {
"name" : "2017",
"value" : 1037.82,
"children" : [ {
"name" : "07",
"value" : 518.91
} ]
} ]
}, ...


The deserialized Java POJO being:



public class GraphDTO {

private Set<Account> accounts = new HashSet<>();

public Set<Account> getAccounts() {
return accounts;
}
}


Questions




  1. How can I remove "accounts" from the generated JSON (first line) ?

  2. Injecting the JSON form into JavaScript, I'm getting an encoded form like:
    var data = { "accounts" : [ { ...
    How can I avoid this ?







javascript java json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 13:36







Hey StackExchange

















asked Nov 26 '18 at 13:20









Hey StackExchangeHey StackExchange

1,325717




1,325717













  • How are you parsing JSON in javascript? Can you provide any code?

    – theapologist
    Nov 26 '18 at 13:22











  • Yes: <script> var data = <c:out value="${graphDTOJSON}"/>; ... </script>

    – Hey StackExchange
    Nov 26 '18 at 13:26



















  • How are you parsing JSON in javascript? Can you provide any code?

    – theapologist
    Nov 26 '18 at 13:22











  • Yes: <script> var data = <c:out value="${graphDTOJSON}"/>; ... </script>

    – Hey StackExchange
    Nov 26 '18 at 13:26

















How are you parsing JSON in javascript? Can you provide any code?

– theapologist
Nov 26 '18 at 13:22





How are you parsing JSON in javascript? Can you provide any code?

– theapologist
Nov 26 '18 at 13:22













Yes: <script> var data = <c:out value="${graphDTOJSON}"/>; ... </script>

– Hey StackExchange
Nov 26 '18 at 13:26





Yes: <script> var data = <c:out value="${graphDTOJSON}"/>; ... </script>

– Hey StackExchange
Nov 26 '18 at 13:26












2 Answers
2






active

oldest

votes


















1














I don't think it's possible to avoid the accounts, but you can do this:



jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts());


We're waiting your way to parse JSON in javascript...






share|improve this answer
























  • Thanks, your approach is giving me the structure I was looking for :) For the use in JavaScript, I do: <script> var data = <c:out value="${graphDTOJSON}"/>; ... </script> to be consume by a sunburst char(docs.anychart.com/Basic_Charts/Sunburst_Chart)

    – Hey StackExchange
    Nov 26 '18 at 13:31











  • Why don't you use JSON.parse(graphDTOJSON) instead?

    – theapologist
    Nov 26 '18 at 13:32











  • Parse will return an Object. Tried your approach with JSON.stringify, don't work. but passing now var data = ${graphDTOJSON}; work.

    – Hey StackExchange
    Nov 26 '18 at 13:53



















0














Got it work as follow:





  1. Applying the answer of Mohicane:



    mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts())




  2. Injecting the JSON from Java to JavaScript (in a JSP) with:



    var data = ${graphDTOJSON};




Many thanks!






share|improve this answer
























  • Is it possible you flag my answer as the right one if you think it's good?

    – Mohicane
    Nov 28 '18 at 16:48











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53482032%2fjson-root-elements-and-encoding%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I don't think it's possible to avoid the accounts, but you can do this:



jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts());


We're waiting your way to parse JSON in javascript...






share|improve this answer
























  • Thanks, your approach is giving me the structure I was looking for :) For the use in JavaScript, I do: <script> var data = <c:out value="${graphDTOJSON}"/>; ... </script> to be consume by a sunburst char(docs.anychart.com/Basic_Charts/Sunburst_Chart)

    – Hey StackExchange
    Nov 26 '18 at 13:31











  • Why don't you use JSON.parse(graphDTOJSON) instead?

    – theapologist
    Nov 26 '18 at 13:32











  • Parse will return an Object. Tried your approach with JSON.stringify, don't work. but passing now var data = ${graphDTOJSON}; work.

    – Hey StackExchange
    Nov 26 '18 at 13:53
















1














I don't think it's possible to avoid the accounts, but you can do this:



jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts());


We're waiting your way to parse JSON in javascript...






share|improve this answer
























  • Thanks, your approach is giving me the structure I was looking for :) For the use in JavaScript, I do: <script> var data = <c:out value="${graphDTOJSON}"/>; ... </script> to be consume by a sunburst char(docs.anychart.com/Basic_Charts/Sunburst_Chart)

    – Hey StackExchange
    Nov 26 '18 at 13:31











  • Why don't you use JSON.parse(graphDTOJSON) instead?

    – theapologist
    Nov 26 '18 at 13:32











  • Parse will return an Object. Tried your approach with JSON.stringify, don't work. but passing now var data = ${graphDTOJSON}; work.

    – Hey StackExchange
    Nov 26 '18 at 13:53














1












1








1







I don't think it's possible to avoid the accounts, but you can do this:



jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts());


We're waiting your way to parse JSON in javascript...






share|improve this answer













I don't think it's possible to avoid the accounts, but you can do this:



jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts());


We're waiting your way to parse JSON in javascript...







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 13:25









MohicaneMohicane

9210




9210













  • Thanks, your approach is giving me the structure I was looking for :) For the use in JavaScript, I do: <script> var data = <c:out value="${graphDTOJSON}"/>; ... </script> to be consume by a sunburst char(docs.anychart.com/Basic_Charts/Sunburst_Chart)

    – Hey StackExchange
    Nov 26 '18 at 13:31











  • Why don't you use JSON.parse(graphDTOJSON) instead?

    – theapologist
    Nov 26 '18 at 13:32











  • Parse will return an Object. Tried your approach with JSON.stringify, don't work. but passing now var data = ${graphDTOJSON}; work.

    – Hey StackExchange
    Nov 26 '18 at 13:53



















  • Thanks, your approach is giving me the structure I was looking for :) For the use in JavaScript, I do: <script> var data = <c:out value="${graphDTOJSON}"/>; ... </script> to be consume by a sunburst char(docs.anychart.com/Basic_Charts/Sunburst_Chart)

    – Hey StackExchange
    Nov 26 '18 at 13:31











  • Why don't you use JSON.parse(graphDTOJSON) instead?

    – theapologist
    Nov 26 '18 at 13:32











  • Parse will return an Object. Tried your approach with JSON.stringify, don't work. but passing now var data = ${graphDTOJSON}; work.

    – Hey StackExchange
    Nov 26 '18 at 13:53

















Thanks, your approach is giving me the structure I was looking for :) For the use in JavaScript, I do: <script> var data = <c:out value="${graphDTOJSON}"/>; ... </script> to be consume by a sunburst char(docs.anychart.com/Basic_Charts/Sunburst_Chart)

– Hey StackExchange
Nov 26 '18 at 13:31





Thanks, your approach is giving me the structure I was looking for :) For the use in JavaScript, I do: <script> var data = <c:out value="${graphDTOJSON}"/>; ... </script> to be consume by a sunburst char(docs.anychart.com/Basic_Charts/Sunburst_Chart)

– Hey StackExchange
Nov 26 '18 at 13:31













Why don't you use JSON.parse(graphDTOJSON) instead?

– theapologist
Nov 26 '18 at 13:32





Why don't you use JSON.parse(graphDTOJSON) instead?

– theapologist
Nov 26 '18 at 13:32













Parse will return an Object. Tried your approach with JSON.stringify, don't work. but passing now var data = ${graphDTOJSON}; work.

– Hey StackExchange
Nov 26 '18 at 13:53





Parse will return an Object. Tried your approach with JSON.stringify, don't work. but passing now var data = ${graphDTOJSON}; work.

– Hey StackExchange
Nov 26 '18 at 13:53













0














Got it work as follow:





  1. Applying the answer of Mohicane:



    mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts())




  2. Injecting the JSON from Java to JavaScript (in a JSP) with:



    var data = ${graphDTOJSON};




Many thanks!






share|improve this answer
























  • Is it possible you flag my answer as the right one if you think it's good?

    – Mohicane
    Nov 28 '18 at 16:48
















0














Got it work as follow:





  1. Applying the answer of Mohicane:



    mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts())




  2. Injecting the JSON from Java to JavaScript (in a JSP) with:



    var data = ${graphDTOJSON};




Many thanks!






share|improve this answer
























  • Is it possible you flag my answer as the right one if you think it's good?

    – Mohicane
    Nov 28 '18 at 16:48














0












0








0







Got it work as follow:





  1. Applying the answer of Mohicane:



    mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts())




  2. Injecting the JSON from Java to JavaScript (in a JSP) with:



    var data = ${graphDTOJSON};




Many thanks!






share|improve this answer













Got it work as follow:





  1. Applying the answer of Mohicane:



    mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts())




  2. Injecting the JSON from Java to JavaScript (in a JSP) with:



    var data = ${graphDTOJSON};




Many thanks!







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 13:57









Hey StackExchangeHey StackExchange

1,325717




1,325717













  • Is it possible you flag my answer as the right one if you think it's good?

    – Mohicane
    Nov 28 '18 at 16:48



















  • Is it possible you flag my answer as the right one if you think it's good?

    – Mohicane
    Nov 28 '18 at 16:48

















Is it possible you flag my answer as the right one if you think it's good?

– Mohicane
Nov 28 '18 at 16:48





Is it possible you flag my answer as the right one if you think it's good?

– Mohicane
Nov 28 '18 at 16:48


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53482032%2fjson-root-elements-and-encoding%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)