Reading JSON sub-arrays dynamically - json

I am using JSON to dynamically populate a parent/child dropdown lists. I have it working to fill the parent drop down and have the child drop down filling with the same parent data. How do I loop through the sub array of the selected value from the parent?
My "each" line (key is the value from my parent DDL:
var key = $(this).prev("input").val();
$.each(jsondata.Id[key].HourTypeCodes, function(i, item) {
My JSON
[{
"Id": 1,
"Title": "Vacation",
"HourTypeCodes": [
{
"Id": "05",
"Title": "VAC POLICE/FIRE"
},
{
"Id": "04",
"Title": "VACATION"
},
{
"Id": "62",
"Title": "VACATION HOURS PURCHASED"
},
{
"Id": "60",
"Title": "VACATION SELL BACK"
}
]
},
{
"Id": 2,
"Title": "Holiday",
"HourTypeCodes": [
{
"Id": "08",
"Title": "HOLIDAY"
}
]
},
{
"Id": 3,
"Title": "Floating Holiday",
"HourTypeCodes": [
{
"Id": "09",
"Title": "FLOATING HOLIDAY"
}
]
}]

I figured it out finally! I found this: http://inderpreetsingh.com/2010/10/14/javascriptjson-find-index-in-an-array-of-objects/
From there these are the two lines that work:
var i = findIndexByKeyValue(jsonRequestType, "Id", key);
$.each(jsonRequestType[i].HourTypeCodes, function(i, item) {

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 read a nested json file in flutter and bind the values to dependent dropdowns

Json file has hierarchy as following:
[
{
"id": 1,
"city": "Mumbai",
"area": [
{
"id": 101,
"name": "Ghatkoapr",
"subArea": [
{
"id": 10101,
"name": "Pantnagar"
}
]
},
]
},
{
"id": 2,
"city": "Pune",
"area": [
{
"id": 201,
"name": "Shivajinagar",
"subArea": [
{
"id": 20101,
"name": "Model Colony"
}
]
}
]
}
]
I want to bind city value to first dropdown and its dependent areas to second dropdown and subareas to third dropdown.
I am new to flutter. I have tried many approaches but didnt help. Can someone help?

lodash sort an array of objects by a property which has an array of objects

I have a an object. I am able to sort the items by using lodash's _.orderBy().
However, in one of the scenario I have to sort by subject, which is an array of objects. Items inside the subject array are already sorted based on the name.
As subject is an array of the objects, I need to consider the first item for sorting.
[
{
"id": "1",
"name": "peter",
"subject": [
{
"id": "1",
"name": "maths"
},
{
"id": "2",
"name": "social"
}
]
},
{
"id": "2",
"name": "david",
"subject": [
{
"id": "2",
"name": "physics"
},
{
"id": "3",
"name": "science"
}
]
},
{
"id": "3",
"name": "Justin",
"subject": [
]
}
]
You can use _.get() to extract the name (or id) of the 1st item in subjects. If no item exists, _.get() will return undefined, which can be replaced with a default value. In this case, we don't want to use an empty string as a default value, since the order would change. Instead I'm checking if the value is a string, if it is I use lower case on it, if not I return it as is.
const arr = [{"id":"1","name":"peter","subject":[{"id":"1","name":"maths"},{"id":"2","name":"social"}]},{"id":"2","name":"david","subject":[{"id":"2","name":"physics"},{"id":"3","name":"science"}]},{"id":"3","name":"Justin","subject":[]}]
const result = _.orderBy(arr, o => {
const name = _.get(o, 'subject[0].name')
return _.isString(name) ? name.toLowerCase() : name
})
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
Use _.sortBy with a comparison/sorting function argument. Your function itself can look into the receiving arguments subject key (I think its the subject you want to compare?)
Since you have the question also tagged with ES6 here is an JS only solution via Array.sort:
let arr = [ { "id": "1", "name": "peter", "subject": [ { "id": "1", "name": "maths" }, { "id": "2", "name": "social" } ] }, { "id": "2", "name": "david", "subject": [ { "id": "2", "name": "physics" }, { "id": "3", "name": "science" } ] }, { "id": "3", "name": "Justin", "subject": [] }, ]
const result = arr.sort((a,b) =>
a.subject.length && b.subject.length
? a.subject[0].name.localeCompare(b.subject[0].name)
: a.subject.length ? -1 : 1)
console.log(result)

JSON Schema for tree structure

I have to build tree like structure of Json data.Each node has an id (an integer, required), a label (a string, optional), and an array of child nodes (optional). Can you help me how to write JSON schema for this Json data. I need to set Id as required in child node as well.
{
"Id": 1,
"Label": "A",
"Child": [
{
"Id": 2,
"Label": "B",
"Child": [
{
"Id": 5,
"Label": "E"
}, {
"Id": 6,
"Label": "E"
}, {
"Id": 7,
"Label": "E"
}
]
}, {
"Id": 3,
"Label": "C"
}, {
"Id": 4,
"Label": "D",
"Child": [
{
"Id": 8,
"Label": "H"
}, {
"Id": 9,
"Label": "I"
}
]
}
]
}
A schema for this structure only needs a definition of a node and a reference to that node. The property Children (renamed from Child) references the node as well.
Here's the schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$ref": "#/definitions/node",
"definitions": {
"node": {
"properties": {
"Id": {
"type": "integer"
},
"Label": {
"type": "string"
},
"Children": {
"type": "array",
"items": {
"$ref": "#/definitions/node"
}
}
},
"required": [
"Id"
]
}
}
}

Dropdown list from google spreadsheet

This fiidle have a sample dropdown list which data is comes from hard coded json format as given below.
var data = [
{
"department": "IT",
"subject": [
{
"title": "Programmer",
"services": [
{
"name": "example1"},
{
"name": "example2"}
]
},
{
"title": "Solutions Architect",
"services": [
{
"name": "example3"},
{
"name": "example4"}
]
},
{
"title": "Database Developer"}
]},
{
"department": "Accounting",
"subject": [
{
"title": "Accountant"},
{
"title": "Payroll Officer"},
{
"title": "Accounts Clerk"},
{
"title": "Analyst"},
{
"title": "Financial Controller"}
]},
{
"department": "HR",
"subject": [
{
"title": "Recruitment Consultant"},
{
"title": "Change Management"},
{
"title": "Industrial Relations"}
]},
{
"department": "Marketing",
"subject": [
{
"title": "Market Researcher"},
{
"title": "Marketing Manager"},
{
"title": "Marketing Co-ordinator"}
]}
]
Is it possible to create the same type of dropdown with data in google spreadsheet? If so, what is the data format required in spreadsheet?
EDIT: Is it possible to create the same dropdown menu using data source something as
var query = new google.visualization.Query("http://spreadsheets.google.com/tq?key=0AozvCNI02VmpdDBKYUhRdVVhb2xGel91c2FkV1daYkE&single=true&gid=0&pub=1");
query.send(handleQueryResponse);
}
function handleQueryResponse(response) {
var data = response.getDataTable();
which is coming from this spreadsheet