how to parse json to list?Looping through the parsed JSON string?Parse JSON into LeadParse nested JSONturn an...
What are the end bytes of *.docx file format
Do the villains know Batman has no superpowers?
How to ask a man to not take up more than one seat on public transport while avoiding conflict?
Exam design: give maximum score per question or not?
When is the earliest in Earth history when Grade 5 Titanium alloy could be made, assuming one knows the formula and has the feedstock?
Is a global DNS record a security risk for phpMyAdmin?
Why do we need to use transistors when building an OR gate?
Microservices and Stored Procedures
I reverse the source code, you negate the output!
Flying pigs arrive
As an employer, can I compel my employees to vote?
How often is duct tape used during crewed space missions?
Escape the labyrinth!
Can Brexit be undone in an emergency?
How should I avoid someone patenting technology in my paper/poster?
Is Zack Morris's 'time stop' ability in "Saved By the Bell" a supernatural ability?
4h 40m delay caused by aircraft inspection, Norwegian refuses EU 261/2004 compensation because it turned out there was nothing wrong with the aircraft
Lead Amalgam as a Material for a Sword
Did HaShem ever command a Navi (Prophet) to break a law?
The relationship of noch nicht and the passive voice
What to do as a player when ranger animal companion dies
Audire, with accusative or dative?
Is it possible to get a pointer to one subobject via a pointer to a different, unreleated subobject?
How is underwater propagation of sound possible?
how to parse json to list?
Looping through the parsed JSON string?Parse JSON into LeadParse nested JSONturn an APEX trigger into scheduled batch updateCannot Parse JSON responseJson Parse IssueParse JSON using APEX provided JSON MethodsParse JSON with Apex
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
{"rates":{"2019-01-03":{"CAD":1.7043139339,"EUR":1.1072725662,"USD":1.2565329081},"2019-01-04":{"CAD":1.7033382229,"EUR":1.111259279,"USD":1.2671689559},"2019-01-02":{"CAD":1.7242832585,"EUR":1.1090778018,"USD":1.2640159707}},"start_at":"2019-01-01","base":"GBP","end_at":"2019-01-05"}
when I try to convert to a list I get an error
public class updateExchangeRates {
public class cls_rate {
public rates rates;
public String base;
public Date date_x;
}
public class rates {
public Decimal CAD;
public Decimal EUR;
public Decimal USD;
}
public class exchangeRateList {
public List<Exchange_Rate__c> cls_rate;
}
public void getExchangeRatesList(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.exchangeratesapi.io/history?start_at=2018-01-01&end_at=2018-02-01&symbols=USD,EUR,CAD&base=GBP');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
//cls_rate rat = new cls_rate();
if (response.getStatusCode() == 200) {
List<Exchange_Rate__c> exchangeList = (exchangeRateList) System.JSON.deserialize(response.getBody().replace('"date":', '"date_x":'), exchangeRateList.class);
}
}
}
apex json
add a comment
|
{"rates":{"2019-01-03":{"CAD":1.7043139339,"EUR":1.1072725662,"USD":1.2565329081},"2019-01-04":{"CAD":1.7033382229,"EUR":1.111259279,"USD":1.2671689559},"2019-01-02":{"CAD":1.7242832585,"EUR":1.1090778018,"USD":1.2640159707}},"start_at":"2019-01-01","base":"GBP","end_at":"2019-01-05"}
when I try to convert to a list I get an error
public class updateExchangeRates {
public class cls_rate {
public rates rates;
public String base;
public Date date_x;
}
public class rates {
public Decimal CAD;
public Decimal EUR;
public Decimal USD;
}
public class exchangeRateList {
public List<Exchange_Rate__c> cls_rate;
}
public void getExchangeRatesList(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.exchangeratesapi.io/history?start_at=2018-01-01&end_at=2018-02-01&symbols=USD,EUR,CAD&base=GBP');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
//cls_rate rat = new cls_rate();
if (response.getStatusCode() == 200) {
List<Exchange_Rate__c> exchangeList = (exchangeRateList) System.JSON.deserialize(response.getBody().replace('"date":', '"date_x":'), exchangeRateList.class);
}
}
}
apex json
2
That's because the JSON itself is not a list structure... it's a map structure ({} rather than []). If you want to make a list out of this, you will need to do some of your own conversion by deserializing it into a map structure first and then looping through the values of the map.
– Chris Johnson
8 hours ago
Why not use this that will generate APEX bean for you?adminbooster.com/tool/json2apex
– Pranay Jaiswal
7 hours ago
I would use this JSON2Apex website to generate a JSON parsing class. And then, I would use a Map to extract the necessary [Key, values] and then easy to extract the necessary values to be my list. Refer this post.
– Arnold Jr.
7 hours ago
add a comment
|
{"rates":{"2019-01-03":{"CAD":1.7043139339,"EUR":1.1072725662,"USD":1.2565329081},"2019-01-04":{"CAD":1.7033382229,"EUR":1.111259279,"USD":1.2671689559},"2019-01-02":{"CAD":1.7242832585,"EUR":1.1090778018,"USD":1.2640159707}},"start_at":"2019-01-01","base":"GBP","end_at":"2019-01-05"}
when I try to convert to a list I get an error
public class updateExchangeRates {
public class cls_rate {
public rates rates;
public String base;
public Date date_x;
}
public class rates {
public Decimal CAD;
public Decimal EUR;
public Decimal USD;
}
public class exchangeRateList {
public List<Exchange_Rate__c> cls_rate;
}
public void getExchangeRatesList(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.exchangeratesapi.io/history?start_at=2018-01-01&end_at=2018-02-01&symbols=USD,EUR,CAD&base=GBP');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
//cls_rate rat = new cls_rate();
if (response.getStatusCode() == 200) {
List<Exchange_Rate__c> exchangeList = (exchangeRateList) System.JSON.deserialize(response.getBody().replace('"date":', '"date_x":'), exchangeRateList.class);
}
}
}
apex json
{"rates":{"2019-01-03":{"CAD":1.7043139339,"EUR":1.1072725662,"USD":1.2565329081},"2019-01-04":{"CAD":1.7033382229,"EUR":1.111259279,"USD":1.2671689559},"2019-01-02":{"CAD":1.7242832585,"EUR":1.1090778018,"USD":1.2640159707}},"start_at":"2019-01-01","base":"GBP","end_at":"2019-01-05"}
when I try to convert to a list I get an error
public class updateExchangeRates {
public class cls_rate {
public rates rates;
public String base;
public Date date_x;
}
public class rates {
public Decimal CAD;
public Decimal EUR;
public Decimal USD;
}
public class exchangeRateList {
public List<Exchange_Rate__c> cls_rate;
}
public void getExchangeRatesList(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.exchangeratesapi.io/history?start_at=2018-01-01&end_at=2018-02-01&symbols=USD,EUR,CAD&base=GBP');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
//cls_rate rat = new cls_rate();
if (response.getStatusCode() == 200) {
List<Exchange_Rate__c> exchangeList = (exchangeRateList) System.JSON.deserialize(response.getBody().replace('"date":', '"date_x":'), exchangeRateList.class);
}
}
}
apex json
apex json
edited 7 hours ago
Adrian Larson♦
117k19 gold badges138 silver badges278 bronze badges
117k19 gold badges138 silver badges278 bronze badges
asked 8 hours ago
YaromYarom
223 bronze badges
223 bronze badges
2
That's because the JSON itself is not a list structure... it's a map structure ({} rather than []). If you want to make a list out of this, you will need to do some of your own conversion by deserializing it into a map structure first and then looping through the values of the map.
– Chris Johnson
8 hours ago
Why not use this that will generate APEX bean for you?adminbooster.com/tool/json2apex
– Pranay Jaiswal
7 hours ago
I would use this JSON2Apex website to generate a JSON parsing class. And then, I would use a Map to extract the necessary [Key, values] and then easy to extract the necessary values to be my list. Refer this post.
– Arnold Jr.
7 hours ago
add a comment
|
2
That's because the JSON itself is not a list structure... it's a map structure ({} rather than []). If you want to make a list out of this, you will need to do some of your own conversion by deserializing it into a map structure first and then looping through the values of the map.
– Chris Johnson
8 hours ago
Why not use this that will generate APEX bean for you?adminbooster.com/tool/json2apex
– Pranay Jaiswal
7 hours ago
I would use this JSON2Apex website to generate a JSON parsing class. And then, I would use a Map to extract the necessary [Key, values] and then easy to extract the necessary values to be my list. Refer this post.
– Arnold Jr.
7 hours ago
2
2
That's because the JSON itself is not a list structure... it's a map structure ({} rather than []). If you want to make a list out of this, you will need to do some of your own conversion by deserializing it into a map structure first and then looping through the values of the map.
– Chris Johnson
8 hours ago
That's because the JSON itself is not a list structure... it's a map structure ({} rather than []). If you want to make a list out of this, you will need to do some of your own conversion by deserializing it into a map structure first and then looping through the values of the map.
– Chris Johnson
8 hours ago
Why not use this that will generate APEX bean for you?adminbooster.com/tool/json2apex
– Pranay Jaiswal
7 hours ago
Why not use this that will generate APEX bean for you?adminbooster.com/tool/json2apex
– Pranay Jaiswal
7 hours ago
I would use this JSON2Apex website to generate a JSON parsing class. And then, I would use a Map to extract the necessary [Key, values] and then easy to extract the necessary values to be my list. Refer this post.
– Arnold Jr.
7 hours ago
I would use this JSON2Apex website to generate a JSON parsing class. And then, I would use a Map to extract the necessary [Key, values] and then easy to extract the necessary values to be my list. Refer this post.
– Arnold Jr.
7 hours ago
add a comment
|
2 Answers
2
active
oldest
votes
Worth putting JSON through e.g. https://jsonformatter.curiousconcept.com/ so the structure is clearer:
{
"rates":{
"2019-01-03":{
"CAD":1.7043139339,
"EUR":1.1072725662,
"USD":1.2565329081
},
"2019-01-04":{
"CAD":1.7033382229,
"EUR":1.111259279,
"USD":1.2671689559
},
"2019-01-02":{
"CAD":1.7242832585,
"EUR":1.1090778018,
"USD":1.2640159707
}
},
"start_at":"2019-01-01",
"base":"GBP",
"end_at":"2019-01-05"
}
Given the date strings used as property names (and no arrays are involved as Bryan says and https://json2apex.herokuapp.com/ doesn't AFAIK handle varying property names), probably easiest to walk through the maps in your code:
class Rate {
String date;
Decimal cad;
Decimal eur;
Decimal usd;
}
Rate[] rates = new Rate[] {};
Map<String, Object> rootMap = (Map<String, Object>) JSON.deserializeUntyped(jsonString);
Map<String, Object> ratesMap = (Map<String, Object>) rootMap.get('rates');
for (String date : ratesMap.keySet() {
Map<String, Object> rateMap = (Map<String, Object>) ratesMap.get(date);
Rate rate = new Rate();
rate.date = date;
rate.cad = (Decimal) rateMap.get('CAD');
rate.eur = (Decimal) rateMap.get('EUR');
rate.usd = (Decimal) rateMap.get('USD');
rates.add(rate);
}
add a comment
|
The JSON being returned is not in a list
[{...},...]
Therefore you cannot cast it as a list
add a comment
|
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "459"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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/4.0/"u003ecc by-sa 4.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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f278424%2fhow-to-parse-json-to-list%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
Worth putting JSON through e.g. https://jsonformatter.curiousconcept.com/ so the structure is clearer:
{
"rates":{
"2019-01-03":{
"CAD":1.7043139339,
"EUR":1.1072725662,
"USD":1.2565329081
},
"2019-01-04":{
"CAD":1.7033382229,
"EUR":1.111259279,
"USD":1.2671689559
},
"2019-01-02":{
"CAD":1.7242832585,
"EUR":1.1090778018,
"USD":1.2640159707
}
},
"start_at":"2019-01-01",
"base":"GBP",
"end_at":"2019-01-05"
}
Given the date strings used as property names (and no arrays are involved as Bryan says and https://json2apex.herokuapp.com/ doesn't AFAIK handle varying property names), probably easiest to walk through the maps in your code:
class Rate {
String date;
Decimal cad;
Decimal eur;
Decimal usd;
}
Rate[] rates = new Rate[] {};
Map<String, Object> rootMap = (Map<String, Object>) JSON.deserializeUntyped(jsonString);
Map<String, Object> ratesMap = (Map<String, Object>) rootMap.get('rates');
for (String date : ratesMap.keySet() {
Map<String, Object> rateMap = (Map<String, Object>) ratesMap.get(date);
Rate rate = new Rate();
rate.date = date;
rate.cad = (Decimal) rateMap.get('CAD');
rate.eur = (Decimal) rateMap.get('EUR');
rate.usd = (Decimal) rateMap.get('USD');
rates.add(rate);
}
add a comment
|
Worth putting JSON through e.g. https://jsonformatter.curiousconcept.com/ so the structure is clearer:
{
"rates":{
"2019-01-03":{
"CAD":1.7043139339,
"EUR":1.1072725662,
"USD":1.2565329081
},
"2019-01-04":{
"CAD":1.7033382229,
"EUR":1.111259279,
"USD":1.2671689559
},
"2019-01-02":{
"CAD":1.7242832585,
"EUR":1.1090778018,
"USD":1.2640159707
}
},
"start_at":"2019-01-01",
"base":"GBP",
"end_at":"2019-01-05"
}
Given the date strings used as property names (and no arrays are involved as Bryan says and https://json2apex.herokuapp.com/ doesn't AFAIK handle varying property names), probably easiest to walk through the maps in your code:
class Rate {
String date;
Decimal cad;
Decimal eur;
Decimal usd;
}
Rate[] rates = new Rate[] {};
Map<String, Object> rootMap = (Map<String, Object>) JSON.deserializeUntyped(jsonString);
Map<String, Object> ratesMap = (Map<String, Object>) rootMap.get('rates');
for (String date : ratesMap.keySet() {
Map<String, Object> rateMap = (Map<String, Object>) ratesMap.get(date);
Rate rate = new Rate();
rate.date = date;
rate.cad = (Decimal) rateMap.get('CAD');
rate.eur = (Decimal) rateMap.get('EUR');
rate.usd = (Decimal) rateMap.get('USD');
rates.add(rate);
}
add a comment
|
Worth putting JSON through e.g. https://jsonformatter.curiousconcept.com/ so the structure is clearer:
{
"rates":{
"2019-01-03":{
"CAD":1.7043139339,
"EUR":1.1072725662,
"USD":1.2565329081
},
"2019-01-04":{
"CAD":1.7033382229,
"EUR":1.111259279,
"USD":1.2671689559
},
"2019-01-02":{
"CAD":1.7242832585,
"EUR":1.1090778018,
"USD":1.2640159707
}
},
"start_at":"2019-01-01",
"base":"GBP",
"end_at":"2019-01-05"
}
Given the date strings used as property names (and no arrays are involved as Bryan says and https://json2apex.herokuapp.com/ doesn't AFAIK handle varying property names), probably easiest to walk through the maps in your code:
class Rate {
String date;
Decimal cad;
Decimal eur;
Decimal usd;
}
Rate[] rates = new Rate[] {};
Map<String, Object> rootMap = (Map<String, Object>) JSON.deserializeUntyped(jsonString);
Map<String, Object> ratesMap = (Map<String, Object>) rootMap.get('rates');
for (String date : ratesMap.keySet() {
Map<String, Object> rateMap = (Map<String, Object>) ratesMap.get(date);
Rate rate = new Rate();
rate.date = date;
rate.cad = (Decimal) rateMap.get('CAD');
rate.eur = (Decimal) rateMap.get('EUR');
rate.usd = (Decimal) rateMap.get('USD');
rates.add(rate);
}
Worth putting JSON through e.g. https://jsonformatter.curiousconcept.com/ so the structure is clearer:
{
"rates":{
"2019-01-03":{
"CAD":1.7043139339,
"EUR":1.1072725662,
"USD":1.2565329081
},
"2019-01-04":{
"CAD":1.7033382229,
"EUR":1.111259279,
"USD":1.2671689559
},
"2019-01-02":{
"CAD":1.7242832585,
"EUR":1.1090778018,
"USD":1.2640159707
}
},
"start_at":"2019-01-01",
"base":"GBP",
"end_at":"2019-01-05"
}
Given the date strings used as property names (and no arrays are involved as Bryan says and https://json2apex.herokuapp.com/ doesn't AFAIK handle varying property names), probably easiest to walk through the maps in your code:
class Rate {
String date;
Decimal cad;
Decimal eur;
Decimal usd;
}
Rate[] rates = new Rate[] {};
Map<String, Object> rootMap = (Map<String, Object>) JSON.deserializeUntyped(jsonString);
Map<String, Object> ratesMap = (Map<String, Object>) rootMap.get('rates');
for (String date : ratesMap.keySet() {
Map<String, Object> rateMap = (Map<String, Object>) ratesMap.get(date);
Rate rate = new Rate();
rate.date = date;
rate.cad = (Decimal) rateMap.get('CAD');
rate.eur = (Decimal) rateMap.get('EUR');
rate.usd = (Decimal) rateMap.get('USD');
rates.add(rate);
}
edited 6 hours ago
answered 6 hours ago
Keith CKeith C
101k14 gold badges105 silver badges244 bronze badges
101k14 gold badges105 silver badges244 bronze badges
add a comment
|
add a comment
|
The JSON being returned is not in a list
[{...},...]
Therefore you cannot cast it as a list
add a comment
|
The JSON being returned is not in a list
[{...},...]
Therefore you cannot cast it as a list
add a comment
|
The JSON being returned is not in a list
[{...},...]
Therefore you cannot cast it as a list
The JSON being returned is not in a list
[{...},...]
Therefore you cannot cast it as a list
answered 8 hours ago
Bryan AndersonBryan Anderson
846 bronze badges
846 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Salesforce Stack Exchange!
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f278424%2fhow-to-parse-json-to-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
That's because the JSON itself is not a list structure... it's a map structure ({} rather than []). If you want to make a list out of this, you will need to do some of your own conversion by deserializing it into a map structure first and then looping through the values of the map.
– Chris Johnson
8 hours ago
Why not use this that will generate APEX bean for you?adminbooster.com/tool/json2apex
– Pranay Jaiswal
7 hours ago
I would use this JSON2Apex website to generate a JSON parsing class. And then, I would use a Map to extract the necessary [Key, values] and then easy to extract the necessary values to be my list. Refer this post.
– Arnold Jr.
7 hours ago