title: Play & JSON: How to make scala code more concise
body: <p>Given the following <code>case class</code>...</p>
  
  <pre><code>case class Address(
    name: Option[String],
    zip: String,
    city: String
  )
  </code></pre>
  
  <p>... I need to generate different JSON depending on whether or not <code>name</code> is defined:</p>
  
  <pre><code>val selector = address.name.map { addressName =&gt;
    Json.obj(
      userId.key -&gt; userId.value,
      "addresses.name" -&gt; name,
      "$or" -&gt; Json.arr(
        Json.obj(s"addresses.name" -&gt; name),
        Json.obj("addresses.name" -&gt; Json.obj("$ne" -&gt; addressName))
      )
    )
  } getOrElse {
    Json.obj(
      userId.key -&gt; userId.value,
      "addresses.name" -&gt; name
    )
  }
  </code></pre>
  
  <p>If you look at the code above, both blocks have a common part:</p>
  
  <pre><code>Json.obj(
    userId.key -&gt; userId.value,
    "addresses.name" -&gt; name
  </code></pre>
  
  <p>Is there any way to make the code more concise without repeating the common part?</p>