Ideas for validating JSON before making an API request

Options
Jozef_783863
Jozef_783863 Posts: 331 admin
edited June 2020 in Questions

I am searching for a common approach when validating JSON before making an API request.

Have others found a similar or better way to do this?

  1. Field: Set value of a text field outputs request-body.
  2. Field: Field Formulas to validate JSON outputs request-body-validated.
  3. Web API: Send POST Request uses one single field placed into Request Body.



With this approach, when a line break is present in request-body, an expected error returns from Field: Field Formulas. The invalid character, the line break, can be seen when pasting request-body into a code editor with JSON syntax highlighting present. This should help a user identify where their request-body may be invalid, so they can more easily troubleshoot an API error response.


Tagged:

Comments

  • Thomas_937381
    Options

    @Jozef_783863 Couldn't you also use Fields: Create fields from JSON? Would this be solved by using entry validation on the fields upfront, or a Fields: Field formulas action to replace line breaks with a null value?

  • Jozef_783863
    Jozef_783863 Posts: 331 admin
    Options

    Now using an updated field formula to consolidate step 1 and 2 (also handles line breaks). Fields: Create fields from JSON outputs using a prefix and we do not have a use for this action type in this context. Entry validation to fields is certainly the recommendation for combating bad data entry like unsupported characters.

  • Kopf_103260
    Options

    Hey @Jozef_783863 ,
    how can I reference the object values afterwards in the request body?

    {{request-body-validated--name}} 
    

    does not work.
    Thanks

  • Jozef_783863
    Jozef_783863 Posts: 331 admin
    Options

    @Kopf_103260, In my second comment, I am told that the field formula compiles a valid JSON body. Then the result of that field formula goes into the API Request body. This approach should work if the display name of the field corresponds with the system name of the field (ex: display name = First Name, system name = first-name).

    In case you are still having trouble, I recommend sharing a dummy example in a new question similar to the example I shared in this question.

  • Kopf_103260
    Options

    Hey @Jozef_783863 ,

    1. Stringify input fields:

    when using JSON.stringify on the inputs from a web form, I get correctly escaped characters: (Javascript Reference Test["test-fields-1"] with output prefix Test)

    {"test-fields-1":"test with \" & more special characters"}
    

    2. My goal is: get the value of field test-fields-1 as correctly displayed in 1. for the API request body

    The request body:

    [
      {
        "op": "add",
        "path": "/fields/Custom.Field1",
        "value": "{{test-fields-1}}"
      }
    ]
    

    should translate to:

    [
      {
        "op": "add",
        "path": "/fields/Custom.Field1",
        "value": "test with \" & more special characters"
      }
    ]
    
    2.1 If I do what you suggested and create fields from the output mentioned in point 1. then all escaped done by stringify are lost again and for Output Field Prefix test-fields-1-to-field I get:
       [ 
         {
           "op": "add",
           "path": "/fields/Custom.Field1",
           "value": "{{test-fields-1-to-field--test-fields-1}}"
         }
       ]
    

    translates to:

       [
         {
           "op": "add",
           "path": "/fields/Custom.Field1",
           "value": "test with " & more special characters"
         }
       ]
    

    This is of course not what I want since it is again not escaped.

    2.2 My question is: How do I use the escaped value of javascript object Test with key test-fields-1 within my request body?

    Reference

    Actions Taken Output field reference
    JSON.stringify global field in field formula Test with key test-fields-1
    Create fields from JSON test-fields-1-to-field generates from key {{test-fields-1-to-field--test-fields-1}}
  • Jeff_146001
    Jeff_146001 Posts: 296 admin
    Options

    @Kopf_103260 @Jozef_783863 Perhaps I'm misunderstanding the need, but I've tested the following, and it's working:

    1. Use the Field: Field Formulas action to encode the text field:

    2. In the Web API action, use the following convention for the request body:

  • Kopf_103260
    Options

    Hey @Jeff_763648 @Jozef_783863,
    yes this works but I have multiple input fields and this would be a lot of work to all stringify them separately in a separate field formula call.
    Isn't there a nicer way to do this?
    I just want to reference the escaped object by key Test["test-fields-1"].

  • Jeff_146001
    Jeff_146001 Posts: 296 admin
    Options

    @Kopf_103260 Since you have a lot of fields, you could encode the entire Request body at once in the Field: Field Formulas action like this:

    result = `
    [
      {
        "op": "add",
        "path": `+JSON.stringify(fields["text-field-path"])+`,
        "value": `+JSON.stringify(fields["text-field-1"])+`
      }
    ]`;
    

    This article describes well what's happening with the ` backtick character.

    Then in the Web API action, simply reference the encoded field:

  • Kopf_103260
    Options

    @Jeff_763648 great solution! This is exactly what I was hoping for and probably helps many others!
    Just one issue: if a field is empty, the result of +JSON.stringify(fields["text-field-1"])+ should be "" but instead is null.
    This throws an error when making the API call.
    I included some screenshots as reference.
    Javascript online editor
    Field Formula for Request Body fails when field is empty
    API Post Request Error

  • Jeff_146001
    Jeff_146001 Posts: 296 admin
    Options

    @Kopf_103260 I think you can solve this with this formula

    result = `
    [
      {
        "op": "add",
        "path": `+JSON.stringify(fields["text-field-path"]||"")+`,
        "value": `+JSON.stringify(fields["text-field-1"]||"")+`
      }
    ]`;
    

    In the formula above, I've added ||"" after each of the field references. || means OR, so when the field value is null, it'll perform the stringify function on "".