Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

How to get meaningful values from json object and to work with them

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, everyone. I work with json object in my c# application. I deserialized the json successfully and what I want is to take only meaningful properties (properties with actual values) from the json and to use them (actually I have to update database tables using these properties).

What I have tried:

For example let's see "TicketsData" class. Here it is:

public class TicketsData
{
public int id { get; set; }
public string ticket_number { get; set; }
public int user_id { get; set; }
public int? origin_id { get; set; }
public int priority_id { get; set; }
public int sla_plan_id { get; set; }
public int status_id { get; set; }
public int? timezone_id { get; set; }
public string is_auto_assigned { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public int? created_by { get; set; }
public string closed_at { get; set; }
public string subject { get; set; }
public string first_assigned_agent_or_team { get; set; }
public string last_assigned_agent_or_team { get; set; }
public string service_fulfill { get; set; }
public int? no_of_workflow_positions { get; set; }
public double? invoice_amount { get; set; }
public object invoiceDetails { get; set; }
}

Part of the json dealing with "TicketsData":

ticketsData
5
updated_at "2018-09-20 14:21:40"
29 {…}

I use reflection in my code:

public void UpdateEntriesFrom_tbl_ts_TicketsData(Dictionary<int, TicketsData> ticketsData)
{
foreach (KeyValuePair<int, TicketsData> entry in ticketsData)
{
Type t = entry.Value.GetType();
PropertyInfo[] props = t.GetProperties();
Dictionary<string, object> dict = new Dictionary<string, object>();
foreach (PropertyInfo prp in props)
{
object value = prp.GetValue(entry.Value, new object[] { });
dict.Add(prp.Name, value);
}

foreach (var item in dict)
{
if (item.Value != null)
{
UpdateEntry("tbl_ts_TicketsData", item.Key, item.Value,
entry.Key);
}
}
}
}

The problems I am facing are two. First problem is that the reflection gets all properties of "TicketsData" class including the "id" and what I need is all properties without "id". The second problem is that if I use the above code, I make a check:

if (item.Value != null)
{
UpdateEntry("tbl_ts_TicketsData", item.Key, item.Value,
entry.Key);
}

but in my db table "tbl_ts_TicketsData" there are fields like "user_id" which are (int, not null) and when json is as the above snippet, "user_id" comes with default value of "0", passes the check

if (item.Value != null)

and triggers the update method, which is not what I want (I want to use only the meaningful fields from the json). I tried another approach without reflection like this:

foreach (KeyValuePair<int, TicketsData> entry in ticketsData)
{
if (entry.Value.user_id != 0)
{
UpdateEntry("tbl_ts_TicketsData", "user_id", entry.Value.user_id, entry.Key);
}
}

but the problem with that approach is thah "0" is a valid digit for "id" column of "User" table and I can miss a case if I use the code above.
Can you help me to get only the meaningful properties (properties with real not defalut values) from the deserialized json so I to be able to use them?
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic