Selects fields from the original JSON.
Selects fields from the original JSON. Typical use case scenario for this method is when one wants to control the full flow from parsing to selecting fields from JSON. Most likely you will use this method in combination with FieldsStringValidator class.
Example:
import org.zalando.sweetcherry.JsonPartialConverter._ ... val jsonString = """{ "uid": "12345", "color": "red", "brand": "nike" }""" FieldsStringValidator.validate("uid,another_field") match { case InvalidFieldsString(error) => // handle string parsing error case ValidFieldsString(fields) => // string is OK and can be parsed into collections of fields Json.parse(jsonString).selectFields(fields) }
Parses input string and selects fields specified in this string.
Parses input string and selects fields specified in this string. Example:
import org.zalando.sweetcherry.JsonPartialConverter._ ... val jsonString = """{ "uid": "12345", "color": "red", "brand": "nike" }""" val json: JsValue = Json.parse(jsonString).selectFields("uid,color") match { case Right(transformedJson) => // will be { "uid": "12345", "color": "red" } transformedJson case Left(error) => // here you can return original json instead of an error Json.parse(s"""{ "error": "${error.message}" }""") }
input fields string
either parser failure with explanation message or JsValue which contains only selected fields. Structure of original JSON will be preserved. You can select nested fields without modification of code of your API clients which most likely will rely on original JSON structure). This method will return original JSON if field string was not specified or when it's empty.