Powershell: How can I dynamically iterate over nested json objects? [duplicate] - json

This question already has answers here:
PowerShell iterate through json of key value pairs
(1 answer)
Powershell Selecting NoteProperty Type Objects From Object
(2 answers)
Closed 3 months ago.
Suppose I have something like this:
"config_service": {
"config_service_1": {
"service": "__service1__"
},
"config_service_2": {
"service": "__service2__"
}
},
I am able to grab out the parent json by doing
$jsonMappings.config_service
But how can I dynamically iterate over each json value in there and do something like, for each object within config_service, extract value of key named 'service'? When I try doing ForEach-Object it treats the config service 1 and 2 as one big object.

Related

Jsp use JSON Object with JSTL [duplicate]

This question already has answers here:
How do I use JSTL to output a value from a JSON string?
(1 answer)
Display JSON object using JSP/ JSTL tags in UI? [duplicate]
(1 answer)
How to parse JSON in Java
(36 answers)
Closed 3 years ago.
I know this topic was discussed pretty often and I guess it is not a diffucult thing.
I want to use a JSON Object from my session in a JSP. The Object has the following structur:
{
"addedUsers":[
{
"city":"Los Angeles",
"name":"Doe",
"forname":"John",
"title":"Dr.",
"userId":2
}
],
"allUsers":[
{
"city":"Los Angeles",
"name":"Doe",
"forname":"John",
"title":"Dr.",
"userId":2
},
{
"city":"New York",
"name":"Peter",
"forname":"Parker",
"title":"Dr.",
"userId":3
}
]
}
Now I want to grab the Objects by name for example doing a for each on the "addedUsers" Object and grab the properties. It is important not just to iterate over the whole Object. I have to call them by name.

unmarshall json using dynamic key [duplicate]

This question already has answers here:
Golang parse a json with DYNAMIC key [duplicate]
(1 answer)
How to parse/deserialize dynamic JSON
(4 answers)
Marshal dynamic JSON field tags in Go
(1 answer)
How to Unmarshal jSON with dynamic key which can't be captured as a `json` in struct: GOlang [duplicate]
(1 answer)
Closed 4 years ago.
I'm receiving a json object that has a known-static structure inside a key that varies between 10 different values.
Consider lastname can be any in a list of 10 lastnames:
var lastnames = [...]string { "Smith", "Johnson", "Williams", "Brown", "Jones", "Miller", "Davis", "Garcia", "Rodriguez", "Wilson" }
Now, this is how the json looks:
{
(lastname here):
{
"position": value,
"user_email": value
}
}
I tried to unmarshall it using the following structs, but I only get null values:
type Inside struct {
Rol string `json:"position"`
Email string `json:"user_email"`
}
type Outside struct {
Key Inside
}
...
var outside Outside
json.Unmarshal([]byte(body), &outside)
Is it possible to unmarshall this directly without creating 10 different structs? Is there possible workaround?

Powershell: ConvertTo-Json isn't working properly while converting entire object [duplicate]

This question already has answers here:
Unexpected array to string conversion
(1 answer)
Cannot convert PSCustomObjects within array back to JSON correctly
(2 answers)
Closed 4 years ago.
My $obj variable has two properties, nodes and rules. There is no issue with nodes, but here is the problem with rules:
# Here, I'm converting the specific "rules" property instead of the entire object.
# This is correct and the output should be exactly like this
$obj.rules | ConvertTo-Json
{
"name": "wildcard",
"description": "match all request",
"include": {
"path": [
"*"
]
}
}
However, converting the entire object to JSON results in this:
$obj | ConvertTo-Json
{
"nodes": {
#Nodes are displayed correctly here
},
"rules": [
{
"name": "wildcard",
"description": "match all request",
"include": "#{path=System.Object[]}"
}
]
Look at the last line, I don't know why this is happening.
I'm trying to convert the entire object and export it to a JSON file.
I've even tried looking into converting the two object properties separately and merging them into one variable in order to export the JSON file but haven't figured that out yet.
Any idea on why this is happening? Workarounds?
Please advise!

Deserialize nested json in VB.NET [duplicate]

This question already has answers here:
VB.net JSON Deserialize
(3 answers)
Closed 6 years ago.
I am using the json.net library and I have this json file I want to deserialize:
{
"location":"/UndefinedTag/UndefinedPos/1480679543072",
"parameters":{
"SwitchPoint1":{
"SamplingRate":null,
"BitLength":16,
"BitOffset":0,
"DataType":"BooleanT"
},
"SwitchPoint2":{
"SamplingRate":null,
"BitLength":16,
"BitOffset":1,
"DataType":"BooleanT"
},
"SensorValue":{
"SamplingRate":null,
"BitLength":14,
"BitOffset":2,
"DataType":"IntegerT"
}
}
}
can anybody help ?
Thank you
Please read Deserializing complex object using Json.NET first (altough it's C#-related). In your case you need several .NET object types defined beforehand:
"parent" one holding location and parameters properties (where parameters represents collection of "named sensors")
"named sensor" representing each specialized sensor type, such as SwitchPoint1, SwitchPoint2 or SensorValue (where each "named sensor" type contains related "sensor value" property)
"sensor value" holding single sensor properties such as SamplingRate, BitLength, etc.

Extracting key values across nested JSON [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
I am trying to extract the values from a nested JSON file that looks like so:
var mymenu = {"menu": [{"page": {"url": "http://foo.bar.com","random stuff": {"junk": "rubbish"}}},{"page": {"feed": "http://foo.bar.com"}},{"menu": [{"submenu": [{"page": {"feed": "http://foo.bar.com"}}]}]}]};
The keys I am trying to extract are the feeds with contain urls. I have tried a for...in loop into the retrieved JSON but I can only get as far as pulling the object that the feed is in resulting in the stringified object as a whole. Is there a way to get just the keys I need from the JSON file?
I just validated your json on JSONLint and it seems to be invalid.
Parse error on line 7:
... } }
----------------------^
Expecting 'STRING'
Try this json, and remember to assign a name to your variable.
var mymenu = {"menu": [{"page": {"url": "http://foo.bar.com","random stuff": {"junk": "rubbish"}}},{"page": {"feed": "http://foo.bar.com"}},{"menu": [{"submenu": [{"page": {"feed": "http://foo.bar.com"}}]}]}]};
Next iterating through the json isn't too bad, just think of it as a multi dimensional array with key value pairs. For instance, if you're running firefox it's a good idea to get firebug.
for(var i = 0; i < mymenu['menu'].length; i)
{
console.log(mymenu['menu'][i]);
}