This question already has answers here:
How to efficiently count the number of keys/properties of an object in JavaScript
(19 answers)
Closed 5 years ago.
I have JSON which looks like this:
{
"item1": {
"category": "fruit",
},
"item2": {
"category": "fruit",
}
}
How can I count the no. of items inside this?
As in your JSON, number of items are the number of keys. Thus, get your JSON in an variable (say obj) and use the following:
var obj = JSON.parse('{"item1":{"category":"fruit",},"item2":{"category":"fruit",}}');
Object.keys(obj).length;
Related
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.
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.
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?
This question already has answers here:
Are multi-line strings allowed in JSON?
(15 answers)
Closed 4 years ago.
Given this Json
{
"myclass": {
"studentname": "myname",
"description": "Student has three more credits to complete\n
he is taking maths\ n
biology this semester "
}
}
I get a Json parser exception for 'description' in jsonlint. Changing the description to accept an array is not an option for me.
Is there any way to define multiline in Json?
\n is the only way. JSON isn't programming language and you can't tell it to concatenate string. It depends on context too.
This will work
{
"myclass": {
"studentname": "myname",
"description": "Student has three more credits to complete\nhe is taking maths\nbiology this semester"
}
}
This will not work
{
"myclass": {
"studentname": "myname",
"description": "Student has three more credits to complete\n
he is taking maths\n
biology this semester"
}
}
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!