0

I would like to return the results of a SOQL query as JSON, but the data seems to be returned as a string.

client = SFDC_Adapter.login
data = client.query("SELECT  MarkupAmount__c, 
                        MarkupPercent__c,
                        Product_Type_Id__c,
                        Product_Type__c 
                        FROM Product_Type__c 
                        WHERE Product_Type_Id__c = #{product_type_id}")
p data

=> [#<Product_Type__c:0x00000001c356f8 @Id=nil, @OwnerId=nil, @IsDeleted=nil, @Name=nil, @CreatedDate=nil, @CreatedById=nil, @LastModifiedDate=nil, @LastModifiedById=nil, @SystemModstamp=nil, @MarkupPercent__c=5.0, @Subscription__c=nil, @Product_Type__c="Research Trip", @MarkupAmount__c=nil, @Product_Type_Id__c=36.0>]

    puts data
=> #<Product_Type__c:0x00000001c356f8>

    puts data.to_json
=> ["#<Product_Type__c:0x00000001c356f8>"]

How do I materialize these results into a JSON object for use in a Restful service?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Johnny
  • 355
  • 4
  • 15

1 Answers1

0

I don't know that gem, but from looking at your output, and glancing at your results, it looks like you got a Product_Type object back.

When you use p or puts, inspect is being used, which is turning the instance into something viewable in a web-page, by using an HTML encoding on it. That's why you see < and > in the output.

Instead, you need to access the values in the object. According to the docs, you can use standard getters or using a hash[key] form to do that:

contact = Contact.find("contact_id")                #=> #
contact = Contact.find_by_Name("John Smith")        #=> dynamic finders!
contacts = Contact.all                              #=> a Databasedotcom::Collection of Contact instances
contacts = Contact.find_all_by_Company("IBM")       #=> a Databasedotcom::Collection of matching Contacts
contact.Name                                        #=> the contact's Name attribute
contact["Name"]                                     #=> same thing
contact.Name = "new name"                           #=> change the contact's Name attribute, in memory
contact["Name"] = "new name"                        #=> same thing
contact.save                                        #=> save the changes to the database
contact.update_attributes "Name" => "newer name",
  "Phone" => "4156543210"                           #=> change several attributes at once and save them
contact.delete                                      #=> delete the contact from the database

Try data['Product_Type_Id'] and you should get 36.0. An alternate way of doing the same thing is data.Product_Type_Id.

Once you have your accessors figured out you can generate JSON using a simple hash or array of hashes. This would generate a hash:

require 'json'

hash = {
    'Id'               => data.Id,
    'OwnerId'          => data.OwnerId,
    'IsDeleted'        => data.IsDeleted,
    'Name'             => data.Name,
    'CreatedDate'      => data.CreatedDate,
    'CreatedById'      => data.CreatedById,
    'LastModifiedDate' => data.LastModifiedDate,
    'LastModifiedById' => data.LastModifiedById,
    'SystemModstamp'   => data.SystemModstamp,
    'MarkupPercent'    => data.MarkupPercent,
    'Subscription'     => data.Subscription,
    'Product_Type'     => data.Product_Type,
    'MarkupAmount'     => data.MarkupAmount,
    'Product_Type_Id'  => data.Product_Type_Id,
  }

puts hash.to_json

I didn't see a to_h or to_hash method which would be a shortcut.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303