Sha256: f1f047a6d0b11a002960ea653e03b73e88f2cb7e43b3ce09e7e8b55421a622e0

Contents?: true

Size: 736 Bytes

Versions: 3

Compression:

Stored size: 736 Bytes

Contents

---
layout: default
title: JSON Field Names
parent: Usage
nav_order: 4
---

# JSON Field Names

JavaScript likes to use camelCase, Ruby's idiom is to use snake_case. This can make make manipulating factory-built objects in ruby ugly. To avoid this, you can call your fields one thing and ask the JSON outputter to rename the field when generating JSON.

```ruby
FakerMaker.factory :vehicle do
  wheels { 4 }
  colour { Faker::Color.color_name }
  engine_capacity(json: 'engineCapacity') { rand( 600..2500 ) }
end

v = FM[:vehicle].build
v.engine_capacity = 125
```

and calls to `as_json` and `to_json` will report the fieldname as `engineCapacity`.

```ruby
v.to_json

=> "{\"wheels\":4,\"colour\":\"blue\",\"engineCapacity\":125}"
```

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
faker_maker-1.1.9 docs/usage/json_field_names.md
faker_maker-1.1.8 docs/usage/json_field_names.md
faker_maker-1.1.7 docs/usage/json_field_names.md