How to get the value by a key from a super nested Json

how to get key and value from json array object in javascript
get value from json object in javascript
python find value in nested dictionary
python find key in nested dictionary
python get all values from nested dictionary
python iterate through nested json
python json get value by key
python json get nested value
"_embedded": {
"cscaia:status_report": {
  "_links": {
    "self": {
      "title": "status_report",
      "name": "status_report",
      "href": "https://api.dxc-dev-aia.hub-1.dev.us.insurance.dxc.com/quotes/ID-mrMxY1Dg/status_report"
    },
    "type": {
      "href": "https://diaas-dev.gtaia-test-domain.net/std-dev-lux-13100/insurance/schemas/quotes/statusReportDocument"
    },
    "up": {
      "href": "https://api.dxc-dev-aia.hub-1.dev.us.insurance.dxc.com/quotes/ID-mrMxY1Dg"
    }
  },
  "consistent": false,
  "messages": [
    {
      "message": "Incomplete attribute",
      "context": [
        {
          "propertyNames": [
            "quote$distributor_id"
          ]
        }
      ],
      "severity": "error",
      "code": "incomplete_attr"
    }
  ]
}

}

Here I need to find out the value of the key "severity" but i don't know the level of it .I don't have the key for its parent as well inside which it is nested as the json will always be dynamic.

I have tried with an extension method. But for nested key i am not getting the result.

public static TType JsonValue<TType>(this JObject obj, string key)
    {
        object result = null; //default to null if nothing is found

        foreach (var item in obj)
        {
            var token = item;

            if (token.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase))
            {
                result = token.Value.ToObject<TType>(); //return the value found
                break;
            }

            if (!obj[token.Key].Children().Any())
                continue;

            var jt = obj[token.Key].ToString();

            if (!jt.StartsWith("["))
            {
                result = JsonValue<TType>(JObject.Parse(jt), key);
            }
            else
            {
                obj[token.Key].Children().ToList().ForEach(x =>
                {
                    //only the first match will be returned
                    result = JsonValue<TType>(JObject.Parse(x.ToString()), key);
                });
            }

            if (result != null)
                break;
        }
        return (TType)result;
    }

You could take a recursive approach and return if a value is found.

function getValue(object, key) {
    var value;

    if (!object || typeof object !== 'object') return;
    if (key in object) return object[key];

    Object.values(object).some(v => {
        value = getValue(v, key)
        return value !== undefined;
    });    

    return value;
}

var data = { _embedded: { "cscaia:status_report": { _links: { self: { title: "status_report", name: "status_report", href: "https://api.dxc-dev-aia.hub-1.dev.us.insurance.dxc.com/quotes/ID-mrMxY1Dg/status_report" }, type: { href: "https://diaas-dev.gtaia-test-domain.net/std-dev-lux-13100/insurance/schemas/quotes/statusReportDocument" }, up: { href: "https://api.dxc-dev-aia.hub-1.dev.us.insurance.dxc.com/quotes/ID-mrMxY1Dg" } }, consistent: false, messages: [{ message: "Incomplete attribute", context: [{ propertyNames: ["quote$distributor_id"] }], severity: "error", code: "incomplete_attr" }] } } };

console.log(getValue(data, 'severity'));
console.log(getValue(data, 'href'));

The Android Tablet Developer's Cookbook, Access each field (key-value pair) in the temporary JSONObject, including the nested JSONObject, and display the information for the two products via the TextView control. protected void onCreate(Bundle savedInstanceState) { super. How do I get values in a nested JSON. Rate this: Please Sign up or sign in to vote. See more: C#. JSON. In the nested JSON: Nested loop to get keys and values.

Just use a recursive function to run through all level.

var _embedded = {
  "cscaia:status_report": {
    "_links": {
      "self": {
        "title": "status_report",
        "name": "status_report",
        "href": "https://api.dxc-dev-aia.hub-1.dev.us.insurance.dxc.com/quotes/ID-mrMxY1Dg/status_report"
      },
      "type": {
        "href": "https://diaas-dev.gtaia-test-domain.net/std-dev-lux-13100/insurance/schemas/quotes/statusReportDocument"
      },
      "up": {
        "href": "https://api.dxc-dev-aia.hub-1.dev.us.insurance.dxc.com/quotes/ID-mrMxY1Dg"
      }
    },
    "consistent": false,
    "messages": [
      {
        "message": "Incomplete attribute",
        "context": [
          {
            "propertyNames": [
              "quote$distributor_id"
            ]
          }
        ],
        "severity": "error",
        "code": "incomplete_attr"
      }
    ]
  }
}

function recursive_search (object) {

  let output = null;

  //check if object is an array or object
  if (Array.isArray (object)) {
  
    //check every array-element recursive
    for (let i in object)
      output = recursive_search (object[i])

  } else
  if (typeof object === 'object') {
  
    //get all keys of object
    let keys = Object.keys (object);
    
    //check every key if it is 'serverity'. if not check every property recursive 
    for (let i in keys)
      if (keys[i] === 'severity') {
      
        output = object[keys[i]];
        break;
      
      } else
      if (typeof object === 'object')
        output = recursive_search (object[keys[i]])
  
  }
  
  return output;

}

//get serverity
serverity = recursive_search (_embedded)

console.log ('serverity = ' + serverity);

How to get value from nested json object in java , How to get value from nested json object in java example. How to get first key of json object in javascript with Example. If a field in a Java It's super easy to create a dynamic object structure with any of the JToken derived JSON. The data​  First read the Json and Deserealize it to Jobj. Next Use Deserealize JasonArray Activity and pass the string as Jobj (“planAdminList”).ToString and store as Jarray. Next Run Foreach for Jarray. Inside ForEach Use Deserealise Json Activity and store the value as Jvalue. the data type should be Jobject.

Assuming you want to achieve this using C# you could try querying all descendends using Newtonsoft.Json.Linq untill you find the right Property.

This should work (demo):

public static T GetJsonPropertyValue<T>(string json, string property)
{
    var parsedJson = JObject.Parse(json);
    var token = parsedJson.Descendants()
        .FirstOrDefault(t => t.Type == JTokenType.Property
            && ((JProperty)t).Name == property);
    return token is JProperty
        ? token.First().Value<T>()
        : token.Value<T>();
}

From your example:

static void Main(string[] args)
{
    var json = @"{
     ""_embedded"": {
        ""cscaia:status_report"": {
            ""_links"": {
                ""self"": {
                    ""title"": ""status_report"",
                    ""name"": ""status_report"",
                    ""href"": ""https://api.dxc-dev-aia.hub-1.dev.us.insurance.dxc.com/quotes/ID-mrMxY1Dg/status_report""
                },
                ""type"": {
                    ""href"": ""https://diaas-dev.gtaia-test-domain.net/std-dev-lux-13100/insurance/schemas/quotes/statusReportDocument""
                },
                ""up"": {
                    ""href"": ""https://api.dxc-dev-aia.hub-1.dev.us.insurance.dxc.com/quotes/ID-mrMxY1Dg""
                }
            },
            ""consistent"": false,
            ""messages"": [
                {
                    ""message"": ""Incomplete attribute"",
                    ""context"": [
                        {
                            ""propertyNames"": [
                                ""quote$distributor_id""
                            ]
                        }
                    ],
                    ""severity"": ""error"",
                    ""code"": ""incomplete_attr""
                }
            ]
        }
    }
}";
    var context = GetJsonPropertyValue<dynamic>(json, "context");
    var severity = GetJsonPropertyValue<string>(json, "severity");
    Console.WriteLine(severity);
    Console.WriteLine(context);
    Console.ReadKey();
}

Managing Big Data in Cloud Computing Environments, The simplest NoSQL databases called key-value stores (or big hash tables) 2014c), called super-columns, where a column contains nested (sub)columns. JSON is similar to XML but it is smaller, faster and easier to parse than XML. Stack Overflow Public questions and answers; Getting data from a deeply nested json object. Accessed JObject values with invalid key value: 0.

dabeng/JSON-Loop: JSON Loop is a super easy to use tool , [Stack Overflow] How do I loop through deeply nested properties of a json object? [Stack Overflow] json find parent node of node where key named “id” equal to “  using JSON keys as python attributes in nested JSON I'm working with nested JSON-like data structures in python 2.7 that I exchange with some foreign perl code. I just want to 'work with' these nested structures of lists and dictionaries in amore pythonic way.

Find all occurences of a key in nested python dictionaries and lists , def find(key, dictionary): for k, v in dictionary.iteritems(): if k == key: yield v elif isinstance(v, dict): for result in Super sweet and clean. Hi Friends, from this JSON i need to extract the value '7.7' which is under the key 'text' under 'distance'. JSON objects are surrounded by curly braces {}. 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). Keys and values are separated by a colon. Each key/value pair is separated by a comma.

JavaScript JSON parse() Method, Any nested objects are transformed before the parent. If the function Return Value: A JSON Object, or Array var obj = JSON.parse(text, function (key, value) { Basically I'm trying to accomplish the reverse of Return Nested Key in Groovy. That is, I have some keys read from the System.properties map for example user.home and corresponding values like C:\User\dpr. Now I want to create a map that reflects this structure to use it in a groovy.text.SimpleTemplateEngine as bindings:

Comments
  • what you are trying to do is it in JS or C# both are opposite
  • I tried in c# but if you could give the solution in js also its fine.@user7417866
  • Getting an error stating that Cannot cast Newtonsoft.Json.Linq.JObject to Newtonsoft.Json.Linq.JToken.
  • So you are having a severity property with a nested object? It works assuming it’s a simple key string value property like your example. Eventually post the Json that gives you the error.
  • Please find the link for the json .for which i am facing issue.xfl.jp/tvBEai
  • I am not sure what you adjusted, but the above method works well with your json file. Here is the demonstration
  • Use the key postal_address$address_category and try its throwing an exception .