I have an array of objects that comes in from an api. The data is in this format:
[
{
"__typename": "directus_fields",
"field": "heading",
"type": "string",
"schema": {
"__typename": "directus_fields_schema",
"max_length": 255,
"is_unique": false,
"is_nullable": true
},
"meta": {
"__typename": "directus_fields_meta",
"group": "hero",
"validation_message": null,
"id": 37,
"required": false,
"validation": null,
"interface": "input",
"hidden": false
}
},
{
"__typename": "directus_fields",
"field": "introduction",
"type": "text",
"schema": {
"__typename": "directus_fields_schema",
"max_length": null,
"is_unique": false,
"is_nullable": true
},
"meta": {
"__typename": "directus_fields_meta",
"group": "about_us",
"validation_message": null,
"id": 38,
"required": false,
"validation": null,
"interface": "input-rich-text-html",
"hidden": false
}
},
{
"__typename": "directus_fields",
"field": "campaign_title",
"type": "string",
"schema": {
"__typename": "directus_fields_schema",
"max_length": 255,
"is_unique": false,
"is_nullable": true
},
"meta": {
"__typename": "directus_fields_meta",
"group": "campaign",
"validation_message": null,
"id": 41,
"required": false,
"validation": null,
"interface": "input",
"hidden": false
}
},
{
"__typename": "directus_fields",
"field": "campaign_duration",
"type": "string",
"schema": {
"__typename": "directus_fields_schema",
"max_length": 255,
"is_unique": false,
"is_nullable": true
},
"meta": {
"__typename": "directus_fields_meta",
"group": "campaign",
"validation_message": null,
"id": 42,
"required": false,
"validation": null,
"interface": "input",
"hidden": false
}
},
{
"__typename": "directus_fields",
"field": "campaign_text",
"type": "text",
"schema": {
"__typename": "directus_fields_schema",
"max_length": null,
"is_unique": false,
"is_nullable": true
},
"meta": {
"__typename": "directus_fields_meta",
"group": "campaign",
"validation_message": null,
"id": 43,
"required": false,
"validation": null,
"interface": "input-rich-text-html",
"hidden": false
}
},
{
"__typename": "directus_fields",
"field": "campaign_image",
"type": "uuid",
"schema": {
"__typename": "directus_fields_schema",
"max_length": 36,
"is_unique": false,
"is_nullable": true
},
"meta": {
"__typename": "directus_fields_meta",
"group": "campaign",
"validation_message": null,
"id": 44,
"required": false,
"validation": null,
"interface": "file-image",
"hidden": false
}
},
{
"__typename": "directus_fields",
"field": "philosophy_title",
"type": "string",
"schema": {
"__typename": "directus_fields_schema",
"max_length": 255,
"is_unique": false,
"is_nullable": true
},
"meta": {
"__typename": "directus_fields_meta",
"group": "philosophy",
"validation_message": null,
"id": 45,
"required": false,
"validation": null,
"interface": "input",
"hidden": false
}
},
{
"__typename": "directus_fields",
"field": "philosophy_text",
"type": "text",
"schema": {
"__typename": "directus_fields_schema",
"max_length": null,
"is_unique": false,
"is_nullable": true
},
"meta": {
"__typename": "directus_fields_meta",
"group": "philosophy",
"validation_message": null,
"id": 46,
"required": false,
"validation": null,
"interface": "input-rich-text-html",
"hidden": false
}
},
{
"__typename": "directus_fields",
"field": "title",
"type": "string",
"schema": {
"__typename": "directus_fields_schema",
"max_length": 255,
"is_unique": false,
"is_nullable": true
},
"meta": {
"__typename": "directus_fields_meta",
"group": "about_us",
"validation_message": null,
"id": 52,
"required": false,
"validation": null,
"interface": "input",
"hidden": false
}
},
{
"__typename": "directus_fields",
"field": "hero_text",
"type": "text",
"schema": {
"__typename": "directus_fields_schema",
"max_length": null,
"is_unique": false,
"is_nullable": true
},
"meta": {
"__typename": "directus_fields_meta",
"group": "hero",
"validation_message": null,
"id": 164,
"required": false,
"validation": null,
"interface": "input-rich-text-html",
"hidden": false
}
}
]
I would like to group the data in the array by the meta group name. All meta group names that are similar should be in a new array object.
I tried this but I couldnt get it to work
const tree = [];
const children = {};
data.forEach((item) => {
const newItem = { ...item } as ParsedMenuItem;
const { id: newItem?.meta?.group, parentId: parentId = 0 } = newItem;
children[id] = children[id] || [];
newItem.childMenu = children[id];
parentId
? (children[parentId] = children[parentId] || []).push(newItem)
: tree.push(newItem);
});
return tree;
I just want all similar meta group name to be under one object. I have no idea on how to go about this due to the nested structure.
You could group your data by meta.group using Array.reduce(). Here is an example:
result = data.reduce((a, i) => {
a[i.meta.group] = a[i.meta.group] || [];
a[i.meta.group].push(i);
return a;
}, {});
console.log(result)
<script>
let data = [{
"__typename": "directus_fields",
"field": "heading",
"type": "string",
"schema": {
"__typename": "directus_fields_schema",
"max_length": 255,
"is_unique": false,
"is_nullable": true
},
"meta": {
"__typename": "directus_fields_meta",
"group": "hero",
"validation_message": null,
"id": 37,
"required": false,
"validation": null,
"interface": "input",
"hidden": false
}
},
{
"__typename": "directus_fields",
"field": "introduction",
"type": "text",
"schema": {
"__typename": "directus_fields_schema",
"max_length": null,
"is_unique": false,
"is_nullable": true
},
"meta": {
"__typename": "directus_fields_meta",
"group": "about_us",
"validation_message": null,
"id": 38,
"required": false,
"validation": null,
"interface": "input-rich-text-html",
"hidden": false
}
},
{
"__typename": "directus_fields",
"field": "campaign_title",
"type": "string",
"schema": {
"__typename": "directus_fields_schema",
"max_length": 255,
"is_unique": false,
"is_nullable": true
},
"meta": {
"__typename": "directus_fields_meta",
"group": "campaign",
"validation_message": null,
"id": 41,
"required": false,
"validation": null,
"interface": "input",
"hidden": false
}
},
{
"__typename": "directus_fields",
"field": "campaign_duration",
"type": "string",
"schema": {
"__typename": "directus_fields_schema",
"max_length": 255,
"is_unique": false,
"is_nullable": true
},
"meta": {
"__typename": "directus_fields_meta",
"group": "campaign",
"validation_message": null,
"id": 42,
"required": false,
"validation": null,
"interface": "input",
"hidden": false
}
},
{
"__typename": "directus_fields",
"field": "campaign_text",
"type": "text",
"schema": {
"__typename": "directus_fields_schema",
"max_length": null,
"is_unique": false,
"is_nullable": true
},
"meta": {
"__typename": "directus_fields_meta",
"group": "campaign",
"validation_message": null,
"id": 43,
"required": false,
"validation": null,
"interface": "input-rich-text-html",
"hidden": false
}
},
{
"__typename": "directus_fields",
"field": "campaign_image",
"type": "uuid",
"schema": {
"__typename": "directus_fields_schema",
"max_length": 36,
"is_unique": false,
"is_nullable": true
},
"meta": {
"__typename": "directus_fields_meta",
"group": "campaign",
"validation_message": null,
"id": 44,
"required": false,
"validation": null,
"interface": "file-image",
"hidden": false
}
},
{
"__typename": "directus_fields",
"field": "philosophy_title",
"type": "string",
"schema": {
"__typename": "directus_fields_schema",
"max_length": 255,
"is_unique": false,
"is_nullable": true
},
"meta": {
"__typename": "directus_fields_meta",
"group": "philosophy",
"validation_message": null,
"id": 45,
"required": false,
"validation": null,
"interface": "input",
"hidden": false
}
},
{
"__typename": "directus_fields",
"field": "philosophy_text",
"type": "text",
"schema": {
"__typename": "directus_fields_schema",
"max_length": null,
"is_unique": false,
"is_nullable": true
},
"meta": {
"__typename": "directus_fields_meta",
"group": "philosophy",
"validation_message": null,
"id": 46,
"required": false,
"validation": null,
"interface": "input-rich-text-html",
"hidden": false
}
},
{
"__typename": "directus_fields",
"field": "title",
"type": "string",
"schema": {
"__typename": "directus_fields_schema",
"max_length": 255,
"is_unique": false,
"is_nullable": true
},
"meta": {
"__typename": "directus_fields_meta",
"group": "about_us",
"validation_message": null,
"id": 52,
"required": false,
"validation": null,
"interface": "input",
"hidden": false
}
},
{
"__typename": "directus_fields",
"field": "hero_text",
"type": "text",
"schema": {
"__typename": "directus_fields_schema",
"max_length": null,
"is_unique": false,
"is_nullable": true
},
"meta": {
"__typename": "directus_fields_meta",
"group": "hero",
"validation_message": null,
"id": 164,
"required": false,
"validation": null,
"interface": "input-rich-text-html",
"hidden": false
}
}
]
</script>
I am receiving a dynamic data string from the python kernel, (however, it's an object but the python kernel can only send it as a sting due to the limitation of the python backend that cant be changed due to a third-party package), below is the console.log output.
[{"model_class": "DataToolParameter", "name": "input_file", "argument": null, "type": "data", "label": "VCF/BCF Data", "help": "", "refresh_on_change": true, "optional": false, "hidden": false, "is_dynamic": false, "value": null, "extensions": ["vcf", "vcf_bgzip", "bcf"], "edam": {"edam_formats": ["format_3016", "format_3016", "format_3020"], "edam_data": ["data_0006", "data_0006", "data_3498"]}, "multiple": false, "options": {"hda": [], "hdca": []}, "default_value": null, "text_value": "No dataset."}, {"model_class": "Section", "name": "sec_required", "type": "section", "title": "Required References", "help": null, "expanded": true, "inputs": [{"model_class": "Conditional", "name": "reference_source", "type": "conditional", "cases": [{"model_class": "ConditionalWhen", "value": "cached", "inputs": [{"model_class": "SelectToolParameter", "name": "fasta_ref", "argument": null, "type": "select", "label": "Reference genome", "help": "", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": true, "value": null, "options": [], "display": null, "multiple": false, "textable": false, "default_value": null, "text_value": "None"}]}, {"model_class": "ConditionalWhen", "value": "history", "inputs": [{"model_class": "DataToolParameter", "name": "fasta_ref", "argument": null, "type": "data", "label": "Reference genome", "help": "", "refresh_on_change": true, "optional": false, "hidden": false, "is_dynamic": false, "value": {"values": [{"id": "0b378429c2c91c4f", "src": "hda"}]}, "extensions": ["fasta"], "edam": {"edam_formats": ["format_1929"], "edam_data": ["data_2044"]}, "multiple": false, "options": {"hda": [{"id": "0b378429c2c91c4f", "hid": 11, "name": "UCSC Main on Human: multiz100way (chrX:15,578,261-15,621,068) (as fasta)", "tags": [], "src": "hda", "keep": false}, {"id": "33d7a2c94fd36485", "hid": 9, "name": "non_ACPs.fasta", "tags": [], "src": "hda", "keep": false}, {"id": "47f5e6f7acd01631", "hid": 3, "name": "Extract MAF blocks on data 2 (as fasta)", "tags": [], "src": "hda", "keep": false}, {"id": "695fd3e24b533dbc", "hid": 2, "name": "UCSC Main on Human: multiz100way (chrX:15,560,138-15,602,945) (as fasta)", "tags": [], "src": "hda", "keep": false}], "hdca": []}, "default_value": {"values": [{"id": "0b378429c2c91c4f", "src": "hda"}]}, "text_value": "No dataset."}]}], "test_param": {"model_class": "SelectToolParameter", "name": "reference_source_selector", "argument": null, "type": "select", "label": "Choose the source for the reference genome", "help": "", "refresh_on_change": true, "optional": false, "hidden": false, "is_dynamic": false, "value": "cached", "options": [["Use a built-in genome", "cached", false], ["Use a genome from the history", "history", false]], "display": null, "multiple": false, "textable": false, "text_value": "Use a built-in genome"}}, {"model_class": "DataToolParameter", "name": "gff_annot", "argument": null, "type": "data", "label": "GFF3 annotation file", "help": "From Ensembl: ftp://ftp.ensembl.org/pub/current_gff3/", "refresh_on_change": true, "optional": false, "hidden": false, "is_dynamic": false, "value": null, "extensions": ["gff3"], "edam": {"edam_formats": ["format_1975"], "edam_data": ["data_1255"]}, "multiple": false, "options": {"hda": [], "hdca": []}, "default_value": null, "text_value": "No dataset."}]}, {"model_class": "Section", "name": "sec_csq_opts", "type": "section", "title": "CSQ Options", "help": null, "expanded": true, "inputs": [{"model_class": "IntegerToolParameter", "name": "ncsq", "argument": null, "type": "integer", "label": "maximum number of consequences to consider per site", "help": "-ncsq 16", "refresh_on_change": false, "min": 1, "max": 50, "optional": false, "hidden": false, "is_dynamic": false, "value": "16", "area": false, "datalist": [], "default_value": "16", "text_value": "16"}, {"model_class": "BooleanToolParameter", "name": "local_csq", "argument": null, "type": "boolean", "label": "run localized predictions considering only one VCF record at a time", "help": "--local-csq switch off haplotype-aware calling, run localized predictions considering only one VCF record at a time", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": false, "value": "false", "truevalue": "--local-csq", "falsevalue": "", "default_value": "false", "text_value": "false"}, {"model_class": "SelectToolParameter", "name": "phase", "argument": null, "type": "select", "label": "phase", "help": "how to construct haplotypes and how to deal with unphased data", "refresh_on_change": false, "optional": true, "hidden": false, "is_dynamic": false, "value": null, "options": [["take GTs as is, create haplotypes regardless of phase (0/1 -> 0|1)", "a", false], ["merge *all* GTs into a single haplotype (0/1 -> 1, 1/2 -> 1)", "m", false], ["require phased GTs, throw an error on unphased het GTs", "r", false], ["create non-reference haplotypes if possible (0/1 -> 1|1, 1/2 -> 1|2)", "R", false], ["skip unphased GTs", "s", false]], "display": null, "multiple": false, "textable": false, "default_value": null, "text_value": "Nothing selected."}, {"model_class": "TextToolParameter", "name": "custom_tag", "argument": null, "type": "text", "label": "use this custom tag to store consequences rather than the default BCSQ tag", "help": "", "refresh_on_change": false, "optional": true, "hidden": false, "is_dynamic": false, "value": "", "area": false, "datalist": [], "default_value": "", "text_value": "Empty."}]}, {"model_class": "Section", "name": "sec_restrict", "type": "section", "title": "Restrict to", "help": null, "expanded": false, "inputs": [{"model_class": "Conditional", "name": "regions", "type": "conditional", "cases": [{"model_class": "ConditionalWhen", "value": "__none__", "inputs": []}, {"model_class": "ConditionalWhen", "value": "regions", "inputs": [{"model_class": "Repeat", "name": "regions", "type": "repeat", "title": "Region Filter", "help": null, "default": 1, "min": 1, "max": "__Infinity__", "inputs": [{"model_class": "TextToolParameter", "name": "chrom", "argument": null, "type": "text", "label": "Region chromosome", "help": "", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": false, "value": "", "area": false, "datalist": []}, {"model_class": "TextToolParameter", "name": "start", "argument": null, "type": "text", "label": "Region start position", "help": "", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": false, "value": "", "area": false, "datalist": []}, {"model_class": "TextToolParameter", "name": "stop", "argument": null, "type": "text", "label": "Region end position", "help": "", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": false, "value": "", "area": false, "datalist": []}], "cache": {}}]}, {"model_class": "ConditionalWhen", "value": "regions_file", "inputs": [{"model_class": "DataToolParameter", "name": "regions_file", "argument": null, "type": "data", "label": "Regions File", "help": "restrict to Regions listed in a file", "refresh_on_change": true, "optional": false, "hidden": false, "is_dynamic": false, "value": {"values": [{"id": "0b378429c2c91c4f", "src": "hda"}]}, "extensions": ["tabular"], "edam": {"edam_formats": ["format_3475"], "edam_data": ["data_0006"]}, "multiple": false, "options": {"hda": [{"id": "0b378429c2c91c4f", "hid": 11, "name": "UCSC Main on Human: multiz100way (chrX:15,578,261-15,621,068) (as interval)", "tags": [], "src": "hda", "keep": false}, {"id": "955b0e088867ea92", "hid": 10, "name": "UCSC Main on Human: knownGene (genome)", "tags": [], "src": "hda", "keep": false}, {"id": "33d7a2c94fd36485", "hid": 9, "name": "non_ACPs.fasta (as tabular)", "tags": [], "src": "hda", "keep": false}, {"id": "3ba2ccc043967969", "hid": 3, "name": "Extract MAF blocks on data 2 (as interval)", "tags": [], "src": "hda", "keep": false}, {"id": "57c61e66a8a42935", "hid": 2, "name": "UCSC Main on Human: multiz100way (chrX:15,560,138-15,602,945) (as interval)", "tags": [], "src": "hda", "keep": false}, {"id": "1d6404f66ed3d72e", "hid": 1, "name": "UCSC Main on Human: knownGene (genome)", "tags": [], "src": "hda", "keep": false}], "hdca": []}, "default_value": {"values": [{"id": "0b378429c2c91c4f", "src": "hda"}]}, "text_value": "No dataset."}]}], "test_param": {"model_class": "SelectToolParameter", "name": "regions_src", "argument": null, "type": "select", "label": "Regions", "help": "", "refresh_on_change": true, "optional": false, "hidden": false, "is_dynamic": false, "value": "__none__", "options": [["Do not restrict to Regions", "__none__", false], ["Specify one or more Region(s) directly", "regions", false], ["Operate on Regions specified in a history dataset", "regions_file", false]], "display": null, "multiple": false, "textable": false, "text_value": "Do not restrict to Regions"}}, {"model_class": "Conditional", "name": "targets", "type": "conditional", "cases": [{"model_class": "ConditionalWhen", "value": "__none__", "inputs": []}, {"model_class": "ConditionalWhen", "value": "targets", "inputs": [{"model_class": "Repeat", "name": "targets", "type": "repeat", "title": "Target Filter", "help": null, "default": 1, "min": 1, "max": "__Infinity__", "inputs": [{"model_class": "TextToolParameter", "name": "chrom", "argument": null, "type": "text", "label": "Target chromosome", "help": "", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": false, "value": "", "area": false, "datalist": []}, {"model_class": "TextToolParameter", "name": "start", "argument": null, "type": "text", "label": "Target start position", "help": "", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": false, "value": "", "area": false, "datalist": []}, {"model_class": "TextToolParameter", "name": "stop", "argument": null, "type": "text", "label": "Target end position", "help": "", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": false, "value": "", "area": false, "datalist": []}], "cache": {}}]}, {"model_class": "ConditionalWhen", "value": "targets_file", "inputs": [{"model_class": "DataToolParameter", "name": "targets_file", "argument": null, "type": "data", "label": "Targets File", "help": "restrict to Targets listed in a file", "refresh_on_change": true, "optional": false, "hidden": false, "is_dynamic": false, "value": {"values": [{"id": "0b378429c2c91c4f", "src": "hda"}]}, "extensions": ["tabular"], "edam": {"edam_formats": ["format_3475"], "edam_data": ["data_0006"]}, "multiple": false, "options": {"hda": [{"id": "0b378429c2c91c4f", "hid": 11, "name": "UCSC Main on Human: multiz100way (chrX:15,578,261-15,621,068) (as interval)", "tags": [], "src": "hda", "keep": false}, {"id": "955b0e088867ea92", "hid": 10, "name": "UCSC Main on Human: knownGene (genome)", "tags": [], "src": "hda", "keep": false}, {"id": "33d7a2c94fd36485", "hid": 9, "name": "non_ACPs.fasta (as tabular)", "tags": [], "src": "hda", "keep": false}, {"id": "3ba2ccc043967969", "hid": 3, "name": "Extract MAF blocks on data 2 (as interval)", "tags": [], "src": "hda", "keep": false}, {"id": "57c61e66a8a42935", "hid": 2, "name": "UCSC Main on Human: multiz100way (chrX:15,560,138-15,602,945) (as interval)", "tags": [], "src": "hda", "keep": false}, {"id": "1d6404f66ed3d72e", "hid": 1, "name": "UCSC Main on Human: knownGene (genome)", "tags": [], "src": "hda", "keep": false}], "hdca": []}, "default_value": {"values": [{"id": "0b378429c2c91c4f", "src": "hda"}]}, "text_value": "No dataset."}]}], "test_param": {"model_class": "SelectToolParameter", "name": "targets_src", "argument": null, "type": "select", "label": "Targets", "help": "", "refresh_on_change": true, "optional": false, "hidden": false, "is_dynamic": false, "value": "__none__", "options": [["Do not restrict to Targets", "__none__", false], ["Specify one or more Target(s) directly", "targets", false], ["Operate on Targets specified in a history dataset", "targets_file", false]], "display": null, "multiple": false, "textable": false, "text_value": "Do not restrict to Targets"}}, {"model_class": "TextToolParameter", "name": "include", "argument": "--include", "type": "text", "label": "Include", "help": "Select sites for which the expression is true", "refresh_on_change": false, "optional": true, "hidden": false, "is_dynamic": false, "value": null, "area": false, "datalist": [], "default_value": null, "text_value": "Not available."}, {"model_class": "TextToolParameter", "name": "exclude", "argument": "--exclude", "type": "text", "label": "Exclude", "help": "Exclude sites for which the expression is true", "refresh_on_change": false, "optional": true, "hidden": false, "is_dynamic": false, "value": null, "area": false, "datalist": [], "default_value": null, "text_value": "Not available."}]}, {"model_class": "Section", "name": "sec_subset", "type": "section", "title": "Subset Options", "help": null, "expanded": false, "inputs": [{"model_class": "TextToolParameter", "name": "samples", "argument": "--samples", "type": "text", "label": "Samples", "help": "Comma separated list of samples to annotate (or exclude)", "refresh_on_change": false, "optional": true, "hidden": false, "is_dynamic": false, "value": "", "area": false, "datalist": [], "default_value": "", "text_value": "Empty."}, {"model_class": "BooleanToolParameter", "name": "invert_samples", "argument": null, "type": "boolean", "label": "Invert Samples", "help": "Inverts the query/filtering applied by Samples (adds \\"^\\" prefix to exclude)", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": false, "value": "false", "truevalue": "^", "falsevalue": "", "default_value": "false", "text_value": "false"}, {"model_class": "DataToolParameter", "name": "samples_file", "argument": "--samples_file", "type": "data", "label": "Samples file", "help": "File of samples to include", "refresh_on_change": true, "optional": true, "hidden": false, "is_dynamic": false, "value": null, "extensions": ["tabular"], "edam": {"edam_formats": ["format_3475"], "edam_data": ["data_0006"]}, "multiple": false, "options": {"hda": [{"id": "0b378429c2c91c4f", "hid": 11, "name": "UCSC Main on Human: multiz100way (chrX:15,578,261-15,621,068) (as interval)", "tags": [], "src": "hda", "keep": false}, {"id": "955b0e088867ea92", "hid": 10, "name": "UCSC Main on Human: knownGene (genome)", "tags": [], "src": "hda", "keep": false}, {"id": "33d7a2c94fd36485", "hid": 9, "name": "non_ACPs.fasta (as tabular)", "tags": [], "src": "hda", "keep": false}, {"id": "3ba2ccc043967969", "hid": 3, "name": "Extract MAF blocks on data 2 (as interval)", "tags": [], "src": "hda", "keep": false}, {"id": "57c61e66a8a42935", "hid": 2, "name": "UCSC Main on Human: multiz100way (chrX:15,560,138-15,602,945) (as interval)", "tags": [], "src": "hda", "keep": false}, {"id": "1d6404f66ed3d72e", "hid": 1, "name": "UCSC Main on Human: knownGene (genome)", "tags": [], "src": "hda", "keep": false}], "hdca": []}, "default_value": null, "text_value": "No dataset."}, {"model_class": "BooleanToolParameter", "name": "invert_samples_file", "argument": null, "type": "boolean", "label": "Invert Samples file", "help": "inverts the query/filtering applied by Samples file", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": false, "value": "false", "truevalue": "^", "falsevalue": "", "default_value": "false", "text_value": "false"}]}, {"model_class": "SelectToolParameter", "name": "output_type", "argument": null, "type": "select", "label": "", "help": "", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": false, "value": "b", "options": [["compressed BCF", "b", false], ["uncompressed VCF", "v", false]], "display": null, "multiple": false, "textable": false, "default_value": "b", "text_value": "compressed BCF"}]
I am trying to convert this dynamic string to an object using
JSON.parse(Kernel_inputs_as_string)
It works perfectly for all the cases except a few in which string contains "^" char. It throws an error.
SyntaxError: Unexpected token ^ in JSON at position 13477 error
On the other hand, when I copy and save the console.log string as input and trying JSON.parse() it work perfectly as described below.
const json = '[{"model_class": "DataToolParameter", "name": "input_file".....}]'
const obj = JSON.parse(json);
console.log(obj[0]);
I'm not able to understand why this error is coming and how to solve it for similar cases where the string has "^" char.
Please help me to solve this error.
Thank you
I am getting JSON via ajax like this
{
"questionTypes": [
{
"id": 1,
"name": "text",
"deleted": false,
"version": 0
},
{
"id": 2,
"name": "rating",
"deleted": false,
"version": 0
},
{
"id": 3,
"name": "boolean",
"deleted": false,
"version": 0
},
{
"id": 4,
"name": "option",
"deleted": false,
"version": 0
}
],
"data": [
{
"id": 1,
"category": "default",
"question": "Staff Courtesy",
"deleted": false,
"version": 0
},
{
"id": 2,
"category": "default",
"question": "Staff Response",
"deleted": false,
"version": 0
},
{
"id": 3,
"category": "default",
"question": "Check In",
"deleted": false,
"version": 0
},
{
"id": 4,
"category": "default",
"question": "Check Out",
"deleted": false,
"version": 0
},
{
"id": 5,
"category": "default",
"question": "Travel Desk",
"deleted": false,
"version": 0
},
{
"id": 6,
"category": "default",
"question": "Door Man",
"deleted": false,
"version": 0
},
{
"id": 14,
"category": "client",
"question": "test question",
"deleted": false,
"version": 0
},
{
"id": 15,
"category": "client",
"question": "test1",
"deleted": false,
"version": 0
},
{
"id": 16,
"category": "client",
"question": "test2",
"deleted": false,
"version": 0
},
{
"id": 17,
"category": "client",
"question": "test2",
"deleted": false,
"version": 0
},
{
"id": 21,
"category": "client",
"question": "ggggg",
"deleted": false,
"version": 0
}
]
}
I want to get the name fields from
"questionTypes": [
{
"id": 1,
"name": "text",
"deleted": false,
"version": 0
},
{
"id": 2,
"name": "rating",
"deleted": false,
"version": 0
},
{
"id": 3,
"name": "boolean",
"deleted": false,
"version": 0
},
{
"id": 4,
"name": "option",
"deleted": false,
"version": 0
}
],
$.each(data1.data.questionTypes, function(index, currPat) {
console.log(currPat.name); }
but it did not work ,I get undefined in console.Can any body tell me how to loop exactly
Demo Fiddle
jQuery
$.each(json.data, function (index, currPat) {
$(tbody).append('<tr><td><b>Question from data: ' + currPat.question + '</b></td><td><b>Names from questionTypes</b><select class="qType"></select></td></tr>');
});
var select = $('.qType');
$(json.questionTypes).each(function (index, currPat) {
$(select).append('<option>' + currPat.name + '</option>');
});
You must loop twice: Once for getting the question and then for getting name.
OK, you can do this ,
$(jsonObject.questionTypes).each(function(i,value){
//get value here using value.name
alert(value.name);
});
And the problem was you were actually extracting questionTypes from datawhich is non existent there.
Fiddle Demo
data has no questionTypes
$.each(data1.questionTypes, function(index, currPat) {
alert(currPat.name);
});
in your code I don't see any username properties,
try to use this code.
$.each(data1.data.questionTypes, function(index, currPat) {
console.log(currPat.name); }
Try this
$.each(json.questionTypes, function (index, currPat) {
div=$('<div/>').text(currPat.name);
$('#jsoncontainter').append(div);
});
http://jsfiddle.net/ardeezstyle/497Tx/1/
For your second part of the problem, here is a solution.
$.each(json.data, function (index, currQuest) {
question=$('<div/>',{'class':'question'}).text(currQuest.question);
answer=$('<div/>',{'class':'answer'});
dropdown=$('<select/>');
$.each(json.questionTypes, function (index, currPat) {
option=$('<option/>').text(currPat.name).appendTo(dropdown);
});
answer.append(dropdown);
$('#jsoncontainter').append(question).append(answer);
});
Fiddle