locked
Rest API middleware suggestions RRS feed

  • Question

  • User-1104215994 posted

    Hello,

    I have implemented a REST API middleware application in Flask. There <g class="gr_ gr_22 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="22" data-gr-id="22">is</g> a Rest API host and a client. In the middle of <g class="gr_ gr_23 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="23" data-gr-id="23">those</g> there is my application which gets <g class="gr_ gr_19 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="19" data-gr-id="19">request</g> from <g class="gr_ gr_20 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="20" data-gr-id="20">client</g> and sends it to Rest API host. Than gets <g class="gr_ gr_21 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="21" data-gr-id="21">response</g> from REST API host and sends it to the client. It works but I would like to implement it with VS Web API. 

    In Flask I am parsing <g class="gr_ gr_25 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="25" data-gr-id="25">json</g> requests/responses and save to <g class="gr_ gr_35 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="35" data-gr-id="35">database</g>.  I am not sure if this is a good idea. I am planning to save the JSON request/response without parsing. What are your opinions? In any conflict situation, I will give JSON request/response messages to the related parties. How would you design your database? What are <g class="gr_ gr_116 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="116" data-gr-id="116">advantages</g>/disadvantages of saving JSON to Database?

    Thanks in advance.

    Best Regards.

    Wednesday, January 16, 2019 5:58 AM

Answers

  • User-474980206 posted
    It is common to parse the json for validation, but if you’re code is just a proxy server, and it only logs the json, then a parse may not be needed. Also depending on the database you pick, it may validate the json.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 16, 2019 3:05 PM
  • User475983607 posted

    Advantages/disadvantages of storing JSON request/response in database?

    Here is a sample JSON response:

    "{\"referenceId\":\"28944118333\",\"productCode\":\"001002461285\",\"quantity\":\"1\",\"initiationResultCode\":\"04\",\"validatedToken\":\"\",\"version\":\"V1\",\"signature\":\"508d79fc7400f0ca81aa877b7718ce1b\",\"applicationCode\":\"52e7cf966b724749a7c4efadc3727ed7\"}"

    Only you can answer this question. 

    If you plan to query the data at a later time then saving the data in a database is an advantage.  Otherwise the data takes up space and is a disadvantage.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 16, 2019 6:21 PM
  • User1724605321 posted

    Hi cenk1536,

    You should use VARCHAR(MAX) or NVARCHAR(MAX) instead to store json string  ,TEXT, NTEXT are deprecated as of SQL Server 2005 . With SQL Server 2016 and Azure SQL, there are a lot of additional native JSON capabilities such as  FOR JSON command to convert output from a query into JSON format :

    https://docs.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server?view=sql-server-2017 

    The nvarchar(max) data type lets you store JSON documents that are up to 2 GB in size. If you're sure that your JSON documents aren't greater than 8 KB, however, we recommend that you use NVARCHAR(4000) instead of NVARCHAR(max) for performance reasons.

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 18, 2019 2:43 AM

All replies

  • User197322208 posted

    Do you retrieve the whole JSON for a document ?

    Do you want to filter more JSON's for a criteria ?

    Do you want to paginate those filters ?

    Do you want to export ( into CSV, WORD, EXCEL, PDF)  parts of the JSON ?

    If yes to all or many, re-think your database in relational / reports way

    Wednesday, January 16, 2019 12:54 PM
  • User-1104215994 posted

    Hi,

    I actually store JSON request/response for logging in case of conflicts. I don't want to filter, paginate or export.

    Best Regards.

    Wednesday, January 16, 2019 1:33 PM
  • User475983607 posted

    I am not sure if this is a good idea. I am planning to save the JSON request/response without parsing.

    I assume this is an ASP.NET Core application as that's the subject of your other threads.  It is impossible to save a request or a response in Web API without parsing the HTTP message. 

    It seems you want a proxy which has nothing to do with ASP.NET 

    Wednesday, January 16, 2019 3:00 PM
  • User-474980206 posted
    It is common to parse the json for validation, but if you’re code is just a proxy server, and it only logs the json, then a parse may not be needed. Also depending on the database you pick, it may validate the json.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 16, 2019 3:05 PM
  • User-1104215994 posted

    Advantages/disadvantages of storing JSON request/response in database?

    Here is a sample JSON response:

    "{\"referenceId\":\"28944118333\",\"productCode\":\"001002461285\",\"quantity\":\"1\",\"initiationResultCode\":\"04\",\"validatedToken\":\"\",\"version\":\"V1\",\"signature\":\"508d79fc7400f0ca81aa877b7718ce1b\",\"applicationCode\":\"52e7cf966b724749a7c4efadc3727ed7\"}"

    Wednesday, January 16, 2019 6:10 PM
  • User475983607 posted

    Advantages/disadvantages of storing JSON request/response in database?

    Here is a sample JSON response:

    "{\"referenceId\":\"28944118333\",\"productCode\":\"001002461285\",\"quantity\":\"1\",\"initiationResultCode\":\"04\",\"validatedToken\":\"\",\"version\":\"V1\",\"signature\":\"508d79fc7400f0ca81aa877b7718ce1b\",\"applicationCode\":\"52e7cf966b724749a7c4efadc3727ed7\"}"

    Only you can answer this question. 

    If you plan to query the data at a later time then saving the data in a database is an advantage.  Otherwise the data takes up space and is a disadvantage.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 16, 2019 6:21 PM
  • User-1104215994 posted

    <g class="gr_ gr_8 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="8" data-gr-id="8">Actually</g> I will not query data unless <g class="gr_ gr_6 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="6" data-gr-id="6">client</g> and host have conflict. If <g class="gr_ gr_7 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="7" data-gr-id="7">client</g> says on this date and time I sent this but in <g class="gr_ gr_9 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="9" data-gr-id="9">response</g> you gave me that. In a <g class="gr_ gr_5 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="5" data-gr-id="5">sitation</g> like <g class="gr_ gr_10 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="10" data-gr-id="10">this</g> I want to save JSON request from client and response from Host just for precaution.

    Should I Convert JSONObject into String and save as TEXT/ VARCHAR in DB?

    String stringToBeInserted = jsonObject.toString();
    //and insert this string into DB

    Wednesday, January 16, 2019 6:25 PM
  • User1724605321 posted

    Hi cenk1536,

    You should use VARCHAR(MAX) or NVARCHAR(MAX) instead to store json string  ,TEXT, NTEXT are deprecated as of SQL Server 2005 . With SQL Server 2016 and Azure SQL, there are a lot of additional native JSON capabilities such as  FOR JSON command to convert output from a query into JSON format :

    https://docs.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server?view=sql-server-2017 

    The nvarchar(max) data type lets you store JSON documents that are up to 2 GB in size. If you're sure that your JSON documents aren't greater than 8 KB, however, we recommend that you use NVARCHAR(4000) instead of NVARCHAR(max) for performance reasons.

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 18, 2019 2:43 AM