function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RajaganagavelRajaganagavel 

Help to retrieve the JSON in workbench

Hello Guys, 

I'm just trying to retrieve the data based on the below JSON format in workbench.
Expected JSON in workbench:
{
"Master":"Product",
"Fields":["ProductName,Price"],
"Data": [["Product A","10"],["Product B","20"]]
}

I have custom object called Product_C with the fields ProductName, Price so in that I'm going to retrieve the records as data in the above format. I have rest API class as below.

@RestResource(urlMapping = '/ProductApi/*')
global with sharing class ProductClass {
 @HttpGet
 global static String  getProduct() {
  List < Product__c > products = [SELECT Product_Name__c, Price__c FROM Product__c LIMIT 10];
  JSONGenerator generator = JSON.createGenerator(true);
  generator.writeStartObject();
  generator.writeFieldName('Master');
  generator.writeString('Product');
  generator.writeFieldName('Fields');
  generator.writeStartArray();
  generator.writeString('Name');
  generator.writeString('Price');
  generator.writeEndArray();
  generator.writeFieldName('Data');
  generator.writeStartArray();
  for (Product__c P: products) {
   generator.writeStartArray();
   generator.writeString(P.Product_Name__c);
   generator.writeNumber(P.Price__c);
   generator.writeEndArray();
  }
  generator.writeEndArray();
  generator.writeEndObject();
String jsonString = generator.getAsString();
return jsonString;
 } 
}


By the above class, I'm getting the data as below format in workbench.

WorkBench Raw Response:

"{\n \"Master\" : \"Product\",\n \"Fields\" : [ \"Name\", \"Price\" ],\n \"Data\" : [ [ \"Crocin\", 40.00 ], [ \"Tetanus\", 90.00 ], [ \"Eucalyptus Oil\", 27.00 ], [ \"Axe Oil\", 69.00 ], [ \"Dola 650\", 89.00 ], [ \"Anaestheia\", 43.00 ], [ \"Inhaler\", 105.00 ], [ \"Pain relief oil1\", 40.00 ], [ \"Pain relief oil2\", 25.00 ], [ \"Pain relief oil3\", 15.00 ] ]\n}"

So help me how the get expected JSON based on my requirement.

Expected JSON in workbench:
{
"Master":"Product",
"Fields":["ProductName,Price"],
"Data": [["Product A","10"],["Product B","20"]]
}

Whereas when executing in the anonymous window, I had the result
Anonymous window result

tabitha tabytabitha taby
I am getting an error while retrieving the JSON in the workbench. What should I do now. Please help me out of this issue.
>> https://1921681254.fun
RajaganagavelRajaganagavel
hi tabitha,

I don't understand your point.