How to find length of JSON? - json

This is my JSON array and how i can find JSON array length.
{"quizlist":[
{
"question":"lion.png",
"option":"lion"
},
{
"question":"tiger.png",
"option":"tiger"
},
{
"question":"cheetah.png",
"option":"cheetah"
},
]}

In your case, it looks like a JS Object already.
So, simply use
var jsObj = {
"quizlist": [{
"question": "lion.png",
"option": "lion"
}, {
"question": "tiger.png",
"option": "tiger"
}, {
"question": "cheetah.png",
"option": "cheetah"
}, ]
}
alert(jsObj.quizlist.length)

Related

Reformat JSON object using Dart

I need to standardize the object response key/values so that they are easier to parse/traverse using the tool I'm integrating.
Starting with the following JSON:
{
"status": true,
"body": {
"phone": "+1 937-830-1167",
"address": "2323 kuhku",
"linkedin": "uhku",
"twitter": "uhukh",
"education": "weeww",
"work_experience": "wewaew",
"write_something_about_you": "yugtyt",
"why_you_think_you_are_good_for_this_job": "kuhhuk",
"write_your_assignment_question": "kuhghuhghj",
"upload_your_attachment": null,
"upload_your_resume_here": null
}
}
Using Dart, what would be the best way to reformat as shown?
{
"status": true,
"body": {
"answers":[
{
"label": "phone",
"answer":"+1 937-830-1167"
},
{
"label": "address",
"answer":"2323 kuhku"
},
{
"label": "linkedin",
"answer": "uhku"
},
{
"label": "twitter",
"answer": "uhku"
},
{
"label": "education",
"answer": "uhku"
},
{
"label": "work_experience",
"answer": "uhku"
},
{
"label": "write_something_about_you",
"answer": "uhku"
},
{
"label": "why_you_think_you_are_good_for_this_job",
"answer": "uhku"
},
{
"label": "write_your_assignment_question",
"answer": "uhku"
},
{
"label": "upload_your_attachment",
"answer": "uhku"
},
{
"label": "write_something_about_you",
"answer": "upload_your_resume_here"
}
]
}
}
I'm somewhat limited by the tool I'm using so this will make it much easier to parse the JSON object with JSON Path as needed.
Looks like something that should be easily doable as:
var json = ... your json ...;
var result = {
"status": json["status"],
"body": [for (var e in json["body"].entries)
{"label": e.key, "answer": e.value}
]
};
Create a new JSON object with the same "status" and a "body" which is a list instead of a map, and for each entry in the original map, create a JSON object with a "label" and "answer" taken from the key and value of the map entry.

how to parse dynamic complex json to dart object or model

{
"transactionId": "11f8ecc05273e35a4eb2dc1a",
"type": "REQUEST_FOR_HH_INTERVIEW",
"answers": {
"selectProvinceDistrictCommuneVillage": {
"value": "01020706"
},
"positionOfOfficial": {
"value": "Province Officer"
},
"enterKhmerName": {
"value": "សុខ"
},
"selectSex": {
"value": "MALE"
},
"dob": {
"value": "1994-06-15T03:27:47.409Z"
},
"areYouMarried": {
"value": "YES"
},
"scanSpousesID": {
"value": "435465"
},
"enterSpousesKhmerName": {
"value": "នារី"
},
"selectSexSpouse": {
"value": "FEMALE"
},
"dobSpouse": {
"value": "1996-08-15T03:27:47.409"
},
"numberOfMales": {
"value": "4"
},
"numberOfFemales": {
"value": "5"
},
"selectReasonForRequesting": {
"value": [
"NATURAL_DISASTER"
]
}
}
}
So this is the JSON I need to parse into the dart model. The problem I am having with this structure is that map inside answers are all dynamic. Also, the number of the maps inside answers is not always the same. For example, the next JSON response can be.
{
"transactionId": "11f8ecc05273e35a4eb2dc1a",
"type": "REQUEST_FOR_HH_INTERVIEW",
"answers": {
"selectCode": {
"value": "01020706"
},
"selectRoomValue": {
"value": "1996-08-15T03:27:47.409"
},
"numberOfFamilyMembers": {
"value": "4"
},
"selectFoods": {
"value": [
"Piza",
"Burger"
]
}
}
}
which is different from the first response. I need to make a dart model that parses both responses.
This is relatively easy to do by using a "sub-model" of Answers which would be stored within the assumed InterviewRequest model.
For example:
class InterviewRequest {
final Answers answers;
final String transactionId;
factory InterviewRequest.fromJson(Map<String, dynamic> json) {
return InterviewRequest(
answers: Answers.fromJson(json['answers']),
transactionId: json['transactionId'] as String,
);
}
}
class Answers {
final List<Answer> answers;
factory Answers.fromJson(Map<String, dynamic> json) {
List answers = [];
for (String question in json.keys)
answers.add(Answer(question, json[key]));
return Answers(answers);
}
}

Fetching the array name when traversing array items in JSONata

I need to fetch the name of the array while traversing the child array items.
for example, if my input looks like
{"title": [
{
"value": "18724-100",
"locale": "en-GB"
},
{
"value": "18724-5",
"locale": "en-GB"
},
{
"value": "18724-99",
"locale": "fr-FR"
}
]}
I need output as
{
"data": [
{
"locale": "en-GB",
"metadata": [
{
"key": "title",
"value": "18724-100"
},
{
"key": "title",
"value": "18724-5"
}
]
},
{
"locale": "fr-FR",
"metadata": {
"key": "title",
"value": "18724-99"
}
}
]
}
I tried following spec in JSONata
{
"data": title{locale: value[]} ~> $each(function($v, $k) {
{
"locale": $k,
"metadata": $v.{"key": ???,"value": $}
}
})
}
Please help me to fill "???" so that I can get the array name
Assuming that the input object will always have a single root-level key you can write your expression like this:
{
"data": title{locale: value[]} ~> $each(function($v, $k) {
{
"locale": $k,
"metadata": $v.{"key": $keys($$)[0],"value": $}
}
})
}
$keys returns an array containing keys in the object. $keys($$) will return all keys in root-level of this array (in this case: "title").
Note that for a following input object:
{"title": [
{
"value": "18724-100",
"locale": "en-GB"
},
{
"value": "18724-5",
"locale": "en-GB"
},
{
"value": "18724-99",
"locale": "fr-FR"
}
],
"foo": 123
}
$keys($$) would return an array of two elements (["title", "foo"]).

How can I change key value pair to an entry in json

I have this JSON:
[
{
"name": "bmu_ftp_ip",
"value": "148.72.213.234"
},
{
"name": "bmu_ftp_path",
"value": "/BMU/"
},
...
]
The expected JSON looks like:
[
{
"bmu_ftp_ip": "148.72.213.234",
},
{
"bmu_ftp_path": "/BMU/",
},
...
]
Does anyone have any idea how to achieve expected JSON?
You can use the code like this:
(()=>{
var obj=[ { "name": "bmu_ftp_ip", "value": "148.72.213.234" }, { "name": "bmu_ftp_path", "value": "/BMU/" },]
obj.forEach((e)=>{
e[e.name]=e.value;
delete e.name;
delete e.value;
})
})()

Linq to Json using Like Clause

I've got an MVC 3 web app and am returning a JSON object which I would like to use Linq against to limit the result returned to the client jquery.
The Json response takes the following structure:
{
"Data": {
"Items": [
{
"Result": {
"Id": "e2ba4912-c387-4f54-b55e-06742a6858db",
"Name": "SomeOtherSetting",
"Value": "500",
"Archived": false
},
"Result": {
"Id": "17c27584-cea8-42c2-b6c4-0b30625ac3ce",
"Name": "Setting2",
"Value": "600",
"Archived": false
},
"Result": {
"Id": "17c27584-cea8-42c2-b6c4-0b30625ac3ce",
"Name": "Setting3",
"Value": "700",
"Archived": false
}
}]
}
}
....
I need to return or grab just the json items that have a Name like 'Setting' for example. In the example, this would return just the 2 Json nodes.
My Linq is very limited and what I have is: Settings is where the Json response is stored.
NewtonSoft.Json.Linq.JObject data = NewtonSoft.Json.Linq.JObject.Parse(Settings);
var result = from p in data["Data"]["Items"].Children()
where (p["Result"]["Name"].Contains("Expenses.Help.Tip"))
select new { Name = (string)p["Result"]["Name"], Value = (string)p["Result"]["Value"] };
When I run this I get nothing in my result. Can anyone help and tell me what I'm doing wrong?
Thanks.
Well, I'm not a Json specialist, but I think your Json structure has some problems.
I tried an online parser, and parsing took only the third result... (try to copy past your code in the left window, and look at JS eval.
Instead of
"Items": [
{
"Result": {
},
"Result": {
},
"Result": {
}
}]
you should have each element of Items array (each 'Result') into {}.
"Items": [
{
{"Result": {
}
},
{"Result": {
}
},
{"Result": {
}
}]
I got it to work by changing your Json file to
{
"Data": {
"Items": [
{
"Result": {
"Id": "e2ba4912-c387-4f54-b55e-06742a6858db",
"Name": "SomeOtherSetting",
"Value": "500",
"Archived": false
}
},
{
"Result": {
"Id": "17c27584-cea8-42c2-b6c4-0b30625ac3ce",
"Name": "Setting2",
"Value": "600",
"Archived": false
}
},
{
"Result": {
"Id": "17c27584-cea8-42c2-b6c4-0b30625ac3ce",
"Name": "Setting3",
"Value": "700",
"Archived": false
}
}]
}
}
using
var data = JObject.Parse(test)["Data"]["Items"].Children()
.Where(m => m["Result"]["Name"].Value<string>().Contains("Setting"))
.Select(m => new
{
Name = m["Result"]["Name"].Value<string>(),
Value = m["Result"]["Value"].Value<int>()
})
.ToList();