-1

Thanks in advance, I have to fetch only "app_temp" field value from the JSON response. I have tried the below code:

global class WeatherForecastBatch implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.stateful {

    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'Select id,City__c from Weather_Forecast__c';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Weather_Forecast__c> weath) {
        List <Weather_Forecast__c> collectionofcity = [
                SELECT Id, City__c 
                FROM Weather_Forecast__c
        ];

        for (Weather_Forecast__c w : collectionofcity) {
            try {
                List <Weather_Forecast__c> inserttemp = new List <Weather_Forecast__c>();
                Http http = new Http();
                HttpRequest request = new HttpRequest();
                PageReference endPoint = new PageReference('https://api.weatherbit.io/v2.0/current');
                endPoint.getParameters().put('city', w.City__c);
                endPoint.getParameters().put('key', 'd3730680f92f4b398eef44fe5e0b8661');
                request.setEndpoint(endPoint.getUrl());
                request.setMethod('GET');
                HttpResponse response = http.send(request);
                system.debug(response.getBody());
                JSONParser parser = JSON.createParser(response.getBody());
                system.debug(parser.nextToken());
                system.debug(parser.getcurrentToken());
                if (response.getStatusCode() == 200) {
                    List<Object> results = (List<Object>) Json.deserializeUntyped(response.getBody());
                    system.debug(results);
                    for (Object obj : results) {
                        Map<String, Object> mapvariable = (Map<String, Object>) obj;
                        System.debug('mapvariable:' + mapvariable);
                        system.debug(mapvariable.get('app_temp'));
                    }
                }
            } catch (Exception e) {
                System.debug('Error:' + e.getMessage() + 'LN:' + e.getLineNumber());
            }
        }
    }

    global void finish(Database.BatchableContext BC) {
    }
}
1
  • You seem to have an odd mixture of JSON.createParser() and JSON.deserializeUntyped(). Including an example of the JSON that you're trying to work with would help. It'd also help if you would be more specific about the problem you're facing. Are you getting an error somewhere? If so, you should include the entirety of the error message (and stack trace). You should edit your question to add these details. – Derek F Sep 5 '19 at 12:51
0

First of all, you do not need to retrieve forcasts with SOQL one more time in execute method, you have it from the start method.

Then, you do not use JSONParser at all, so it is better to erase it from your code to clean the one.

Try the following example and make sure you have app_temp in this path:

[
  {
    "app_temp": "some_val"
  }
]

The code:

global class WeatherForecastBatch implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.stateful {

    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'Select id, City__c from Weather_Forecast__c';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Weather_Forecast__c> collectionofcity) {
        for (Weather_Forecast__c w : collectionofcity) {
            try {
                List <Weather_Forecast__c> inserttemp = new List <Weather_Forecast__c>();
                Http http = new Http();
                HttpRequest request = new HttpRequest();
                PageReference endPoint = new PageReference('https://api.weatherbit.io/v2.0/current');
                endPoint.getParameters().put('city', w.City__c);
                endPoint.getParameters().put('key','d3730680f92f4b398eef44fe5e0b8661');
                request.setEndpoint(endPoint.getUrl());
                request.setMethod('GET');
                HttpResponse response = http.send(request);
                system.debug(response.getBody());
                if (response.getStatusCode() == 200) {
                    List<Object> results = (List<Object>) Json.deserializeUntyped(response.getBody());
                    for (Object obj : results) {
                        Map<String, Object> mapvariable = (Map<String, Object>) obj;
                        System.debug('mapvariable:' + mapvariable);
                        system.debug(mapvariable.get('app_temp'));
                    }
                }
            } catch (Exception e) {
                System.debug('Error:' + e.getMessage() + 'LN:' + e.getLineNumber());
            }
        }
    }

    global void finish(Database.BatchableContext BC) {
    }
}

Please, add your response debug to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.