Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Removing a field from jSon String

Removing a field from jSon String

Scheduled Pinned Locked Moved C#
sharepointjsonhelpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    simpledeveloper
    wrote on last edited by
    #1

    I am storing jSon string as hexa-decimal or some characters string, its all working fine whan I retrieving as json string in my code, but there is one field I want to remove from the jSob object after I retrieved.

    var t = StoredProcedures.SP_GlobalSearch.Execute(new SP_GlobalSearch_Params
    {
    Search = request.search,
    SourceKey = "",
    Skip = start,
    Take = length,
    ShowDeleted = false
    }).Select(x => new SP_GlobalSearch
    {
    JsonObject = x.Data != null ? new JavaScriptSerializer().DeserializeObject(x.Data.Replace("", "")) : null,
    Date = x.Date,
    JsonObjectId = x.JsonObjectId,
    SourceKey = x.SourceKey,
    SourceId = x.SourceId,
    TotalCount = x.TotalCount
    });

                        var response = t?.ToList();
    
                        var jsondata = new DataTableResponse
                        {
                            totalCount = (int)response.Where(x => x.TotalCount != null).FirstOrDefault().TotalCount,
                            data = response.Where(x => x.TotalCount == null)
                        };
                       
                        var jsonResult = Json(jsondata);
                        jsonResult.MaxJsonLength = Int32.MaxValue;
                        return jsonResult;
    

    I am reading the text as jSon with the below statement, JavaScriptSerializer().DeserializeObject(x.Data.Replace("", "")) I want to remove the violations field from it, any help please. Thanks in advance Here is my json:

    {
    "InspectionResultId":846,
    "InspectionResultNumber":"T00846",
    "InspectionRequestId":507,
    "InspectionRequestNumber":"R00507",
    "CaseId":689,
    "InspectionResultStatusId":605,
    "EnforcementSectionId":104,
    "ViolationTypeId":603,
    "DateOfInspection":"\/Date(1589439600000)\/",
    "InspectionComment":"send to office staff, open investigation",
    "InspectedCompanyDataId":964,
    "ContactTypeId":701,
    "EnteredById":"7f54fa3e-b5cd-4b2e-9490-92f64c022246",
    "InspectionResultTimestamp":"\/Date(1588280817470)\/",
    "DateCreated":"\/Date(1588280817487)\/",

    OriginalGriffO J 2 Replies Last reply
    0
    • S simpledeveloper

      I am storing jSon string as hexa-decimal or some characters string, its all working fine whan I retrieving as json string in my code, but there is one field I want to remove from the jSob object after I retrieved.

      var t = StoredProcedures.SP_GlobalSearch.Execute(new SP_GlobalSearch_Params
      {
      Search = request.search,
      SourceKey = "",
      Skip = start,
      Take = length,
      ShowDeleted = false
      }).Select(x => new SP_GlobalSearch
      {
      JsonObject = x.Data != null ? new JavaScriptSerializer().DeserializeObject(x.Data.Replace("", "")) : null,
      Date = x.Date,
      JsonObjectId = x.JsonObjectId,
      SourceKey = x.SourceKey,
      SourceId = x.SourceId,
      TotalCount = x.TotalCount
      });

                          var response = t?.ToList();
      
                          var jsondata = new DataTableResponse
                          {
                              totalCount = (int)response.Where(x => x.TotalCount != null).FirstOrDefault().TotalCount,
                              data = response.Where(x => x.TotalCount == null)
                          };
                         
                          var jsonResult = Json(jsondata);
                          jsonResult.MaxJsonLength = Int32.MaxValue;
                          return jsonResult;
      

      I am reading the text as jSon with the below statement, JavaScriptSerializer().DeserializeObject(x.Data.Replace("", "")) I want to remove the violations field from it, any help please. Thanks in advance Here is my json:

      {
      "InspectionResultId":846,
      "InspectionResultNumber":"T00846",
      "InspectionRequestId":507,
      "InspectionRequestNumber":"R00507",
      "CaseId":689,
      "InspectionResultStatusId":605,
      "EnforcementSectionId":104,
      "ViolationTypeId":603,
      "DateOfInspection":"\/Date(1589439600000)\/",
      "InspectionComment":"send to office staff, open investigation",
      "InspectedCompanyDataId":964,
      "ContactTypeId":701,
      "EnteredById":"7f54fa3e-b5cd-4b2e-9490-92f64c022246",
      "InspectionResultTimestamp":"\/Date(1588280817470)\/",
      "DateCreated":"\/Date(1588280817487)\/",

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      You can't "remove" the violations from the JSON once you have read it: either it's still in the JSON string (in which case you probably want to look at a Regex or similar to remove fit from that) or it's a part of the InspectionItem class and the best you can do is set the InspectionItem.Violations property to null for each instance in your data. You can't "delete" a property from a class after compilation! Probably the best way would be to use a Regex to remove the Violations section from the JSON source before you deserialize it - but there could be some significant work involved in writing that, given that the sample you show does not contain any data in the Violations section, so we have no idea how complicated that JSON clause might be when filled in. Complicated clause == complicated Regex! :laugh:

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      S 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        You can't "remove" the violations from the JSON once you have read it: either it's still in the JSON string (in which case you probably want to look at a Regex or similar to remove fit from that) or it's a part of the InspectionItem class and the best you can do is set the InspectionItem.Violations property to null for each instance in your data. You can't "delete" a property from a class after compilation! Probably the best way would be to use a Regex to remove the Violations section from the JSON source before you deserialize it - but there could be some significant work involved in writing that, given that the sample you show does not contain any data in the Violations section, so we have no idea how complicated that JSON clause might be when filled in. Complicated clause == complicated Regex! :laugh:

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

        S Offline
        S Offline
        simpledeveloper
        wrote on last edited by
        #3

        Yeah making that property value null is fine - can you please help me in this regards please? Or can you please help me with the Regex please? Thank you.

        OriginalGriffO 1 Reply Last reply
        0
        • S simpledeveloper

          Yeah making that property value null is fine - can you please help me in this regards please? Or can you please help me with the Regex please? Thank you.

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          You don't know how to set a property? Why are you playing with JSON if you don't know the (very, very) basics?

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • S simpledeveloper

            I am storing jSon string as hexa-decimal or some characters string, its all working fine whan I retrieving as json string in my code, but there is one field I want to remove from the jSob object after I retrieved.

            var t = StoredProcedures.SP_GlobalSearch.Execute(new SP_GlobalSearch_Params
            {
            Search = request.search,
            SourceKey = "",
            Skip = start,
            Take = length,
            ShowDeleted = false
            }).Select(x => new SP_GlobalSearch
            {
            JsonObject = x.Data != null ? new JavaScriptSerializer().DeserializeObject(x.Data.Replace("", "")) : null,
            Date = x.Date,
            JsonObjectId = x.JsonObjectId,
            SourceKey = x.SourceKey,
            SourceId = x.SourceId,
            TotalCount = x.TotalCount
            });

                                var response = t?.ToList();
            
                                var jsondata = new DataTableResponse
                                {
                                    totalCount = (int)response.Where(x => x.TotalCount != null).FirstOrDefault().TotalCount,
                                    data = response.Where(x => x.TotalCount == null)
                                };
                               
                                var jsonResult = Json(jsondata);
                                jsonResult.MaxJsonLength = Int32.MaxValue;
                                return jsonResult;
            

            I am reading the text as jSon with the below statement, JavaScriptSerializer().DeserializeObject(x.Data.Replace("", "")) I want to remove the violations field from it, any help please. Thanks in advance Here is my json:

            {
            "InspectionResultId":846,
            "InspectionResultNumber":"T00846",
            "InspectionRequestId":507,
            "InspectionRequestNumber":"R00507",
            "CaseId":689,
            "InspectionResultStatusId":605,
            "EnforcementSectionId":104,
            "ViolationTypeId":603,
            "DateOfInspection":"\/Date(1589439600000)\/",
            "InspectionComment":"send to office staff, open investigation",
            "InspectedCompanyDataId":964,
            "ContactTypeId":701,
            "EnteredById":"7f54fa3e-b5cd-4b2e-9490-92f64c022246",
            "InspectionResultTimestamp":"\/Date(1588280817470)\/",
            "DateCreated":"\/Date(1588280817487)\/",

            J Offline
            J Offline
            James Curran
            wrote on last edited by
            #5

            To answer this we need to know the type of `SP_GlobalSearch.JsonObject`. If it's something like a `InspectionResult` class or even a `dynamic`, then it's simple: var response = t==null? null : t.Select(r=>{r.InspectionItems.Violations = null; return r;}).ToList(); // purists will hate this ^^^^ If `JsonObject` is a System.Object, then things get messy. Best to change than to one of the options given above.

            Truth, James

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups