3

I'm trying to understand how to use the Jbuilder methods inline in a class. I want to force an object into an array of length one to match the expected JSON spec.

Here is an example of the results I want (note the [] wrapping the value associated with sets):

{
  "sets": [{
    "set_type": "default_set_type",
    "items": [
      {
        "item_id": "FFFF-0000-111",
        "quantity": "1"
      }
    ]
  }]
}

Here is my method so far:

def to_3pl
  @shipment = self
  ...
  Jbuilder.new do |shipment|

    # How do I force jbuilder to wrap a single set with []?
    shipment.sets do
      shipment.set_type 'default_set_type'
      shipment.items @shipment.product_options do |product|
        shipment.item_id product.product_id.to_s
        shipment.quantity product.quantity.to_s
      end
    end
  end
end

And here is the JSON produced by my method (note that the value associated with sets is not wrapped with []):

{
  "sets": {
    "set_type": "default_set_type",
    "items": [
      {
        "item_id": "FFFF-0000-111",
        "quantity": "1"
      }
    ]
  }
}

I've looked through the Jbuilder docs, and am sure there's a way to do this, but I can't seem to figure it out. What is the syntax to force Jbuilder to wrap a single element with [] in a class method?

EDIT WITH SOLUTION

Many thanks to @dddd1919. Here's an updated method with the array wrapper successfully implemented:

def to_3pl
  @shipment = self
  ...
  Jbuilder.new do |shipment|

    # Forces jbuilder to wrap the object with []
    shipment.sets Jbuilder.new.array!(['']) do |set|
      shipment.set_type 'default_set_type'
      shipment.items @shipment.product_options do |product|
        shipment.item_id product.product_id.to_s
        shipment.quantity product.quantity.to_s
      end
    end
  end
end

1 Answer 1

3

If shipment.sets is a list, you can use Jbuilder#array! to serialize data to json array like:

def to_3pl
  @shipment = self
  ...
  Jbuilder.new do |shipment|

    # How do I force jbuilder to wrap a single set with []?
    shipment.sets do
      Jbuilder.new.array!(shipment.sets) do |set|
        ....
      end
    end
  end
end
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the help, @dddd. Sorry if the question is worded poorly, this is the first time I've used Jbuilder and I'm probably missing something. @shipment is a real ActiveRecord object, shipment is just the alias for the Jbuilder wrapper. sets is not a relationship or property of any object in my rails app, it's just a part of the JSON spec I'm trying to export my actual shipment to. The JSON spec requires an array of sets, so the goal is to just force an insert of the array notation []. Does that make sense?
If just serialize to an array data, instead the params of Jbuilder#array! to [''] like Jbuilder.new.array!(['']) do ..., then use your above codes.Is that your means to do?
Awesome. It works- I've updated the code sample to show the working solution with your suggestion. I hate just blindly implementing the fix without understanding what's going on in the syntax- care to help me understand how this is being interpreted?
Jbuilder docs has told your how to write a jbuilder template, code in templete as json is just a instance of Jbuilder, so if you see a function like json.array!(), you can opposite use Jbuilder.new.array!() in your ruby script as euqal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.