Get a specific value from a json structure

get specific value from json
get specific value from json java
get specific value from json c#
json format
get value from json string c# newtonsoft
get specific value from json python
how to get key value from json object in c#
get json node value c#

I have this json:

{
    "unashamedohio": 
        {
            "id": 107537,
            "name": "UnashamedOhio",
            "profileIconId": 785,
            "revisionDate": 1439997758000,
            "summonerLevel": 30
        }
}

I want to get the field named summonerLevel.

I have tried to convert this json to a string and then search for summonerLevel, but I know that this solution is not okay.

I'm using Json.NET.

You can use the dynamic keyword

dynamic obj = JsonConvert.DeserializeObject(json);
Console.WriteLine(obj.unashamedohio.summonerLevel);

How to get specific value from json string, I suggest to create models to present your JSON data: Hide Copy Code. public class MajorIssue { public int attributeId { get; set; } public string  I am iterating a JSON and I want to extract the following fields from this object: Id, Open_date User Ticket_status End_date The data structure that I have is like the following : filtered_d

I'm assuming this json is stored in a string, let's say called json... so try

string json = "...";
JObject obj = JsonConvert.DeserializeObject<JObject>(json);
JObject innerObj = obj["unashamedohio"] as JObject;
int lolSummorLvl = (int) innerObj["summonerLevel"];

Access and print a specific JSON value, but it's important to know anyway. Getting a specific property from a JSON response object; Printing a JSON value to the page; Get the value from an array  Extracts a scalar value from a JSON string. To extract an object or an array from a JSON string instead of a scalar value, see JSON_QUERY (Transact-SQL). For info about the differences between JSON_VALUE and JSON_QUERY, see Compare JSON_VALUE and JSON_QUERY.

You have a couple of possibilities (as already showed in the other answers). Another possibility would be to use the JObject and JProperty properties provided from Json.Net, in order to directly fetch the value like this:

var jsonObject = (JObject)JsonConvert.DeserializeObject(json);
var unashamedohio = (JObject)(jsonObject.Property("unashamedohio").Value);
var summonerLevel = unashamedohio.Property("summonerLevel");
Console.WriteLine(summonerLevel.Value);

Yet another possibility would be to create a typed model of the JSON structure:

public class AnonymousClass
{
    public UnashamedOhio unashamedohio { get; set; }    
}

public class UnashamedOhio
{
    public int summonerLevel { get; set; }
}

and use it to retrieve the value:

var ao = JsonConvert.DeserializeObject<AnonymousClass>(json);
Console.WriteLine(ao.unashamedohio.summonerLevel);

Both solutions print the same value: 30.

IMO you should use always typed models when possible and if you do a lot of value fetching from a JSON structures. It provides error checking in the IDE (as opposed to dynamic) which pay-offs at runtime.

JSON Objects, JSON objects are written in key/value pairs. Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null). Stack Overflow Public questions or retrieve child nodes values from JSON structure in C#. and you can traverse it as needed to extract the specific value(s

JSON Arrays, In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined. Arrays in JSON Objects. Let us take a real life example on the implementation of the JSON in python. A good source for practice purpose is JSON_placeholder, it provides great API requests package which we will be using in our example. To get started, follow these simple steps. Open Python IDE or CLI and create a new script file, name it sample.py.

Extract specific values from nested JSOn output - Build, Hello All, I need to get specific values from output json. Like from below example I want to get value of 'phyMarks'. { "data": { "accounts":  Your answer won't work because the code will not know ahead of time how the JSON is structured. It could be that Darts is in slot 0 and Clean Toilet in slot 1, but it could also be that Clean Toilet is in slot 0 and Darts is in slot 1. Therefore I need some way to match objects on the Name field.

Get a Value by Key in a JSONArray, In the previous example: Firstly, we iterate through the entire list of objects in a JSON array; Then for each JSONObject, we get the value mapped  What you get from the url is a json string. And your can't parse it with index directly. You should convert it to a dict by json.loads and then you can parse it with index. Instead of using .read() to intermediately save it to memory and then read it to json, allow json to load it directly from the file: wjdata = json.load(urllib2.urlopen('url'))

Comments
  • There are lots of questions on Stack Overflow about parsing JSON. You could parse it to an object where you've created an appropriate class beforehand, or use Json.NET's "LINQ to JSON", or any number of things. Please show what you've tried so far.
  • You can search in the string. The other alternative might be to deserialize to another object with only the data you care about, but that's not a great idea either. Is there any reason you can't deserialize the JSON into a whole object, then just check the value on the object?
  • Thanks, this work quite ok. Might have to read more about dynamic objects. Thanks to all btw
  • I really like dynamic and there are situations where using it it's the only solution, but IMO it should be use with caution.
  • Cannot implicitly convert type 'System.Collections.Generic.Dictionary<string,dynamic>' to 'Newtonsoft.Json.Linq.JObject'2013\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs. Thats the error im getting in the Jobject obj line
  • sounds like your json is stored in a dictionary, not a string.