I want to read only the inner Key & Value pairs and want to ignore the parent object heirarchy like “Logging->LogLevel”, “ConnectionStrings”, “AppSettings”, “AzureAd”.
Also these Object names and their heirarchylevel is not consistent across different JSON files.
Example I want only to print/read Key1 & Value1, Key2 & Value2, but need to ignore Logging & LogLevel.
Also like that only print Key3 & Value3, but ignore the object “ConnectionStrings”.
Also would like to ignore the commented lines.
JSON file:
{
"Logging": {
"LogLevel": {
"Key1": "Value1",
"Key2": "Value2"
}
},
"ConnectionStrings": {
"Key3": "Value3"
},
"AppSettings": {
/// Commented Line
"Key4": "Value4"
//////////
//Commented Line
},
"AzureAd": {
"Key5": "Value5"
}
}
Output should like:
"Key1": "Value1",
"Key2": "Value2",
"Key3": "Value3",
"Key4": "Value4",
"Key5": "Value5"
JitendraKumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
It sounds like you want to print leaf nodes of the dictionary. If you were to do this in python, perhaps something like this recursive function would suffice. (or adapt to your language of preference):
def print_leaves(obj: dict):
for key, value in obj.items(): # Iterate over key-value pairs
if isinstance(value, dict): # If this is a dictionary, recurse deeper
print_leaves(value)
else:
print(key, value) # This is a leaf node, print it
a = {
"Logging": {
"LogLevel": {
"Key1": "Value1",
"Key2": "Value2",
}
},
"ConnectionStrings": {
"Key3": "Value3"
},
"AppSettings": {
"Key4": "Value4",
},
"AzureAd": {
"Key5": "Value5",
}
}
print_leaves(a)
Output:
Key1 Value1
Key2 Value2
Key3 Value3
Key4 Value4
Key5 Value5