Hello friends I have one JSON as below
"specifications": [
{
"validationPattern": null,
"itemType": "commerceItem",
"readAccessRight": null,
"securityMaskingValue": null,
"type": "shortText",
"writeSecurityLevel": null,
"required": false,
"writable": true,
"default": null,
"audienceVisibility": null,
"textSearchable": false,
"writeRole": null,
"id": "c_promotionIds",
"dimension": false,
"shopperWriteable": false,
"readRole": null,
"readSecurityLevel": null,
"length": 254,
"shopperReadable": false,
"label": "PromotionIds",
"writeAccessRight": null,
"searchable": false,
"uiEditorType": "shortText",
"localizable": false,
"multiSelect": null
},
{
"validationPattern": null,
"itemType": "commerceItem",
"readAccessRight": null,
"securityMaskingValue": null,
"type": "shortText",
"writeSecurityLevel": null,
"required": false,
"writable": true,
"default": null,
"audienceVisibility": null,
"textSearchable": false,
"writeRole": null,
"id": "c_productType",
"dimension": false,
"shopperWriteable": false,
"readRole": null,
"readSecurityLevel": null,
"length": 254,
"shopperReadable": false,
"label": "ProductType",
"writeAccessRight": null,
"searchable": false,
"uiEditorType": "shortText",
"localizable": false,
"multiSelect": null
},
{
"validationPattern": null,
"itemType": "commerceItem",
"readAccessRight": null,
"securityMaskingValue": null,
"type": "shortText",
"writeSecurityLevel": null,
"required": false,
"writable": true,
"default": null,
"audienceVisibility": null,
"textSearchable": false,
"writeRole": null,
"id": "c_itemStatus",
"dimension": false,
"shopperWriteable": false,
"readRole": null,
"readSecurityLevel": null,
"length": 254,
"shopperReadable": false,
"label": "Item Status",
"writeAccessRight": null,
"searchable": false,
"uiEditorType": "shortText",
"localizable": false,
"multiSelect": null
},
{
"validationPattern": null,
"itemType": "commerceItem",
"readAccessRight": null,
"securityMaskingValue": null,
"type": "number",
"writeSecurityLevel": null,
"required": false,
"writable": true,
"default": null,
"minValue": -9.99999999999E11,
"audienceVisibility": null,
"textSearchable": false,
"writeRole": null,
"id": "c_promotionAmount",
"dimension": false,
"shopperWriteable": false,
"readRole": null,
"readSecurityLevel": null,
"maxValue": 9.99999999999E11,
"length": 12,
"shopperReadable": false,
"label": "PromotionAmount",
"writeAccessRight": null,
"searchable": false,
"uiEditorType": "number",
"localizable": false,
"multiSelect": null
},
{
"validationPattern": null,
"itemType": "commerceItem",
"readAccessRight": null,
"securityMaskingValue": null,
"type": "shortText",
"writeSecurityLevel": null,
"required": false,
"writable": true,
"default": null,
"audienceVisibility": null,
"textSearchable": false,
"writeRole": null,
"id": "c_inventoryLocation",
"dimension": false,
"shopperWriteable": false,
"readRole": null,
"readSecurityLevel": null,
"length": 254,
"shopperReadable": false,
"label": "Inventory Location",
"writeAccessRight": null,
"searchable": false,
"uiEditorType": "shortText",
"localizable": false,
"multiSelect": null
},
{
"validationPattern": null,
"itemType": "commerceItem",
"readAccessRight": null,
"securityMaskingValue": null,
"type": "number",
"writeSecurityLevel": null,
"required": false,
"writable": true,
"default": null,
"minValue": -9.99999999999E11,
"audienceVisibility": null,
"textSearchable": false,
"writeRole": null,
"id": "c_couponAmount",
"dimension": false,
"shopperWriteable": false,
"readRole": null,
"readSecurityLevel": null,
"maxValue": 9.99999999999E11,
"length": 12,
"shopperReadable": false,
"label": "CouponAmount",
"writeAccessRight": null,
"searchable": false,
"uiEditorType": "number",
"localizable": false,
"multiSelect": null
},
{
"validationPattern": null,
"itemType": "commerceItem",
"readAccessRight": null,
"securityMaskingValue": null,
"type": "shortText",
"writeSecurityLevel": null,
"required": false,
"writable": true,
"default": null,
"audienceVisibility": null,
"textSearchable": false,
"writeRole": null,
"id": "c_couponCodes",
"dimension": false,
"shopperWriteable": false,
"readRole": null,
"readSecurityLevel": null,
"length": 254,
"shopperReadable": false,
"label": "CouponCodes",
"writeAccessRight": null,
"searchable": false,
"uiEditorType": "shortText",
"localizable": false,
"multiSelect": null
},
{
"validationPattern": null,
"itemType": "commerceItem",
"readAccessRight": null,
"securityMaskingValue": null,
"type": "shortText",
"writeSecurityLevel": null,
"required": false,
"writable": true,
"default": null,
"audienceVisibility": null,
"textSearchable": false,
"writeRole": null,
"id": "c_couponPromotionIds",
"dimension": false,
"shopperWriteable": false,
"readRole": null,
"readSecurityLevel": null,
"length": 254,
"shopperReadable": false,
"label": "CouponPromotionIds",
"writeAccessRight": null,
"searchable": false,
"uiEditorType": "shortText",
"localizable": false,
"multiSelect": null
},
{
"validationPattern": null,
"itemType": "commerceItem",
"readAccessRight": null,
"securityMaskingValue": null,
"type": "checkbox",
"writeSecurityLevel": null,
"required": false,
"writable": true,
"default": null,
"audienceVisibility": null,
"textSearchable": false,
"writeRole": null,
"id": "c_isINSTALLATIONREQUIRED",
"dimension": false,
"shopperWriteable": false,
"readRole": null,
"readSecurityLevel": null,
"length": 19,
"shopperReadable": false,
"label": "IsInstallationRequired",
"writeAccessRight": null,
"searchable": false,
"uiEditorType": "checkbox",
"localizable": false,
"multiSelect": null
}
]
i need to construct object like below
{
"c_promotionIds" : null,
"c_productType" : null,
"c_itemStatus" :null,
"c_promotionAmount" : null,
"c_inventoryLocation" : null,
"c_couponAmount" : null,
"c_couponCodes" : null,
"c_couponPromotionIds" : null,
"c_isINSTALLATIONREQUIRED" :null
}
I tried like below but not working
let comObject = {},
for (let index = 0; index < responseData.specifications.length; index++) {
comObject = {
responseData.specifications[index].id : null,
}
}
any idea how i can construct this?
You are overwriting the object on each iteration of the array, you need to add new keys to the object and keep the ones you have already assigned.
Also, to indicate that id is the key, you need to sorround it with brackets []
let comObject = {};
for (let index = 0; index < responseData.specifications.length; index++) {
comObject = {
...comObject,
[responseData.specifications[index].id] : null,
}
}
There's a method in the array prototype called reduce that is designed for this kind of operation.
Try this:
const specifications = [
{
"validationPattern": null,
"itemType": "commerceItem",
"readAccessRight": null,
"securityMaskingValue": null,
"type": "shortText",
"writeSecurityLevel": null,
"required": false,
"writable": true,
"default": null,
"audienceVisibility": null,
"textSearchable": false,
"writeRole": null,
"id": "c_promotionIds",
"dimension": false,
"shopperWriteable": false,
"readRole": null,
"readSecurityLevel": null,
"length": 254,
"shopperReadable": false,
"label": "PromotionIds",
"writeAccessRight": null,
"searchable": false,
"uiEditorType": "shortText",
"localizable": false,
"multiSelect": null
},
{
"validationPattern": null,
"itemType": "commerceItem",
"readAccessRight": null,
"securityMaskingValue": null,
"type": "shortText",
"writeSecurityLevel": null,
"required": false,
"writable": true,
"default": null,
"audienceVisibility": null,
"textSearchable": false,
"writeRole": null,
"id": "c_productType",
"dimension": false,
"shopperWriteable": false,
"readRole": null,
"readSecurityLevel": null,
"length": 254,
"shopperReadable": false,
"label": "ProductType",
"writeAccessRight": null,
"searchable": false,
"uiEditorType": "shortText",
"localizable": false,
"multiSelect": null
},
{
"validationPattern": null,
"itemType": "commerceItem",
"readAccessRight": null,
"securityMaskingValue": null,
"type": "shortText",
"writeSecurityLevel": null,
"required": false,
"writable": true,
"default": null,
"audienceVisibility": null,
"textSearchable": false,
"writeRole": null,
"id": "c_itemStatus",
"dimension": false,
"shopperWriteable": false,
"readRole": null,
"readSecurityLevel": null,
"length": 254,
"shopperReadable": false,
"label": "Item Status",
"writeAccessRight": null,
"searchable": false,
"uiEditorType": "shortText",
"localizable": false,
"multiSelect": null
},
{
"validationPattern": null,
"itemType": "commerceItem",
"readAccessRight": null,
"securityMaskingValue": null,
"type": "number",
"writeSecurityLevel": null,
"required": false,
"writable": true,
"default": null,
"minValue": -9.99999999999E11,
"audienceVisibility": null,
"textSearchable": false,
"writeRole": null,
"id": "c_promotionAmount",
"dimension": false,
"shopperWriteable": false,
"readRole": null,
"readSecurityLevel": null,
"maxValue": 9.99999999999E11,
"length": 12,
"shopperReadable": false,
"label": "PromotionAmount",
"writeAccessRight": null,
"searchable": false,
"uiEditorType": "number",
"localizable": false,
"multiSelect": null
},
{
"validationPattern": null,
"itemType": "commerceItem",
"readAccessRight": null,
"securityMaskingValue": null,
"type": "shortText",
"writeSecurityLevel": null,
"required": false,
"writable": true,
"default": null,
"audienceVisibility": null,
"textSearchable": false,
"writeRole": null,
"id": "c_inventoryLocation",
"dimension": false,
"shopperWriteable": false,
"readRole": null,
"readSecurityLevel": null,
"length": 254,
"shopperReadable": false,
"label": "Inventory Location",
"writeAccessRight": null,
"searchable": false,
"uiEditorType": "shortText",
"localizable": false,
"multiSelect": null
},
{
"validationPattern": null,
"itemType": "commerceItem",
"readAccessRight": null,
"securityMaskingValue": null,
"type": "number",
"writeSecurityLevel": null,
"required": false,
"writable": true,
"default": null,
"minValue": -9.99999999999E11,
"audienceVisibility": null,
"textSearchable": false,
"writeRole": null,
"id": "c_couponAmount",
"dimension": false,
"shopperWriteable": false,
"readRole": null,
"readSecurityLevel": null,
"maxValue": 9.99999999999E11,
"length": 12,
"shopperReadable": false,
"label": "CouponAmount",
"writeAccessRight": null,
"searchable": false,
"uiEditorType": "number",
"localizable": false,
"multiSelect": null
},
{
"validationPattern": null,
"itemType": "commerceItem",
"readAccessRight": null,
"securityMaskingValue": null,
"type": "shortText",
"writeSecurityLevel": null,
"required": false,
"writable": true,
"default": null,
"audienceVisibility": null,
"textSearchable": false,
"writeRole": null,
"id": "c_couponCodes",
"dimension": false,
"shopperWriteable": false,
"readRole": null,
"readSecurityLevel": null,
"length": 254,
"shopperReadable": false,
"label": "CouponCodes",
"writeAccessRight": null,
"searchable": false,
"uiEditorType": "shortText",
"localizable": false,
"multiSelect": null
},
{
"validationPattern": null,
"itemType": "commerceItem",
"readAccessRight": null,
"securityMaskingValue": null,
"type": "shortText",
"writeSecurityLevel": null,
"required": false,
"writable": true,
"default": null,
"audienceVisibility": null,
"textSearchable": false,
"writeRole": null,
"id": "c_couponPromotionIds",
"dimension": false,
"shopperWriteable": false,
"readRole": null,
"readSecurityLevel": null,
"length": 254,
"shopperReadable": false,
"label": "CouponPromotionIds",
"writeAccessRight": null,
"searchable": false,
"uiEditorType": "shortText",
"localizable": false,
"multiSelect": null
},
{
"validationPattern": null,
"itemType": "commerceItem",
"readAccessRight": null,
"securityMaskingValue": null,
"type": "checkbox",
"writeSecurityLevel": null,
"required": false,
"writable": true,
"default": null,
"audienceVisibility": null,
"textSearchable": false,
"writeRole": null,
"id": "c_isINSTALLATIONREQUIRED",
"dimension": false,
"shopperWriteable": false,
"readRole": null,
"readSecurityLevel": null,
"length": 19,
"shopperReadable": false,
"label": "IsInstallationRequired",
"writeAccessRight": null,
"searchable": false,
"uiEditorType": "checkbox",
"localizable": false,
"multiSelect": null
}
];
const result = {};
specifications.forEach(spec => result[spec.id] = null);
console.log(result);
Related
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 want to know what is the example data from invoice.upcoming webhook of stripe.
Is the data.object.id is null?
Where I can get the amount? Is it on the data.object.amount_paid or data.object.amount_due?
And what is the status of this invoice?
Example data I got here in stackoverflow but I think it's not complete?
{
"created": 1326853478,
"livemode": false,
"id": "evt_000000000010000",
"type": "invoice.upcoming",
"object": "event",
"request": null,
"pending_webhooks": 1,
"api_version": "2017-06-05",
"data": {
"object": {
"id": null,
"object": "invoice",
"amount_due": 0,
"amount_paid": 0,
"application_fee": null,
"attempt_count": 0,
"attempted": true,
"charge": null,
"closed": true,
"currency": "usd",
"customer": "cus_J1QXR23OrUTxx1fk",
"date": 1503541536,
"description": null,
"discount": null,
"ending_balance": 0,
"forgiven": false,
"lines": {
"data": [
{
"id": "sub_BNx15yNiTkAlQOye",
"object": "line_item",
"amount": 500,
"currency": "jpy",
"description": null,
"discountable": true,
"livemode": true,
"metadata": {
},
"period": {
"start": 1507604796,
"end": 1510283196
},
"plan": {
"id": "bplan",
"object": "plan",
"amount": 500,
"created": 1504352393,
"currency": "jpy",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {
},
"name": "B plan",
"statement_descriptor": null,
"trial_period_days": null
},
"proration": false,
"quantity": 1,
"subscription": null,
"subscription_item": "si_1B0LmKE9P3qCpf5erqbpMxkI",
"type": "subscription"
}
],
"total_count": 1,
"object": "list",
"url": "/v1/invoices/in_1AuB2KE9P3qCpf5ekFh7qpAi/lines"
},
"livemode": false,
"metadata": {
},
"next_payment_attempt": null,
"paid": true,
"period_end": 1503541536,
"period_start": 1503541536,
"receipt_number": null,
"starting_balance": 0,
"statement_descriptor": null,
"subscription": "sub2JQx1xlg9zIFhk",
"subtotal": 0,
"tax": null,
"tax_percent": null,
"total": 0,
"webhooks_delivered_at": 1503541537
}
}
}
Those Events generated from the Dashboard are totally fake, so I don't think you'll ever find usable data in them, but amount_due is what you want here.
Since Upcoming Invoices haven't been created yet - they're just a preview of future charges - they have no ID or status.
Here's a full example of Stripe's invoice.upcoming event.
I found it using the test clocks feature. Please note that is the kind of data you'll get by renewing a yearly susbscription.
{
"object": "invoice",
"account_country": "US",
"account_name": "Your company",
"account_tax_ids": null,
"amount_due": 9900,
"amount_paid": 0,
"amount_remaining": 9900,
"application_fee_amount": null,
"attempt_count": 0,
"attempted": false,
"automatic_tax": {
"enabled": false,
"status": null
},
"billing_reason": "upcoming",
"charge": null,
"collection_method": "charge_automatically",
"created": 1679397051,
"currency": "usd",
"custom_fields": null,
"customer": "cus_XXXXXXXX",
"customer_address": null,
"customer_email": "xxxx#user.io",
"customer_name": "Customer name",
"customer_phone": null,
"customer_shipping": null,
"customer_tax_exempt": "none",
"customer_tax_ids": [],
"default_payment_method": null,
"default_source": null,
"default_tax_rates": [],
"description": null,
"discount": null,
"discounts": [],
"due_date": null,
"ending_balance": 0,
"footer": null,
"last_finalization_error": null,
"lines": {
"object": "list",
"data": [
{
"id": "il_tmp_xxxxxxxxxxxxxxxxxxxxxxx",
"object": "line_item",
"amount": 9900,
"currency": "usd",
"description": "1 × Your sub (at $99.00 / year)",
"discount_amounts": [],
"discountable": true,
"discounts": [],
"livemode": false,
"metadata": {},
"period": {
"end": 1711019451,
"start": 1679397051
},
"plan": {
"id": "price_XXXXXXXXXXXXXXXX",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 9900,
"amount_decimal": "9900",
"billing_scheme": "per_unit",
"created": 1600678675,
"currency": "usd",
"interval": "year",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_XXXXXXXXXXXX",
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"price": {
"id": "price_XXXXXXXXXXXXXX",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": 1600678675,
"currency": "usd",
"livemode": false,
"lookup_key": null,
"metadata": {},
"nickname": null,
"product": "prod_XXXXXXXXXXXX",
"recurring": {
"aggregate_usage": null,
"interval": "year",
"interval_count": 1,
"trial_period_days": null,
"usage_type": "licensed"
},
"tax_behavior": "unspecified",
"tiers_mode": null,
"transform_quantity": null,
"type": "recurring",
"unit_amount": 9900,
"unit_amount_decimal": "9900"
},
"proration": false,
"proration_details": {
"credited_items": null
},
"quantity": 1,
"subscription": "sub_XXXXXXXXXXXXXX",
"subscription_item": "si_XXXXXXXXXXXX",
"tax_amounts": [],
"tax_rates": [],
"type": "subscription"
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/invoices/upcoming/lines?customer=cus_XXXXXXXXXXXX&subscription=sub_XXXXXXXXXXX"
},
"livemode": false,
"metadata": {},
"next_payment_attempt": 1679400651,
"number": null,
"on_behalf_of": null,
"paid": false,
"paid_out_of_band": false,
"payment_intent": null,
"payment_settings": {
"payment_method_options": null,
"payment_method_types": null
},
"period_end": 1679397051,
"period_start": 1647861051,
"post_payment_credit_notes_amount": 0,
"pre_payment_credit_notes_amount": 0,
"quote": null,
"receipt_number": null,
"starting_balance": 0,
"statement_descriptor": null,
"status": "draft",
"status_transitions": {
"finalized_at": null,
"marked_uncollectible_at": null,
"paid_at": null,
"voided_at": null
},
"subscription": "sub_XXXXXXXXXXXX",
"subtotal": 9900,
"tax": null,
"test_clock": "clock_1Kfj8pAP8XE2C2mat1YQW7Rg",
"total": 9900,
"total_discount_amounts": [],
"total_tax_amounts": [],
"transfer_data": null,
"webhooks_delivered_at": null
}
I'm trying to get and display data (twitter feed) on my html page.
I have PHP page that connects to twitter API and creates this output:
[{
"created_at": "Sun Apr 09 17:56:20 +0000 2017",
"id": 851131679167139842,
"id_str": "851131679167139842",
"text": "hello world with an image goes here as a test! https:\/\/t.co\/jNfdESxPcn",
"truncated": false,
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": [],
"media": [{
"id": 851131665623732225,
"id_str": "851131665623732225",
"indices": [47, 70],
"media_url": "http:\/\/pbs.twimg.com\/media\/C8_TvYqW0AEbBaP.jpg",
"media_url_https": "https:\/\/pbs.twimg.com\/media\/C8_TvYqW0AEbBaP.jpg",
"url": "https:\/\/t.co\/jNfdESxPcn",
"display_url": "pic.twitter.com\/jNfdESxPcn",
"expanded_url": "https:\/\/twitter.com\/DavidHoperz\/status\/851131679167139842\/photo\/1",
"type": "photo",
"sizes": {
"thumb": {
"w": 150,
"h": 150,
"resize": "crop"
},
"large": {
"w": 670,
"h": 728,
"resize": "fit"
},
"small": {
"w": 626,
"h": 680,
"resize": "fit"
},
"medium": {
"w": 670,
"h": 728,
"resize": "fit"
}
}
}]
},
"extended_entities": {
"media": [{
"id": 851131665623732225,
"id_str": "851131665623732225",
"indices": [47, 70],
"media_url": "http:\/\/pbs.twimg.com\/media\/C8_TvYqW0AEbBaP.jpg",
"media_url_https": "https:\/\/pbs.twimg.com\/media\/C8_TvYqW0AEbBaP.jpg",
"url": "https:\/\/t.co\/jNfdESxPcn",
"display_url": "pic.twitter.com\/jNfdESxPcn",
"expanded_url": "https:\/\/twitter.com\/DavidHoperz\/status\/851131679167139842\/photo\/1",
"type": "photo",
"sizes": {
"thumb": {
"w": 150,
"h": 150,
"resize": "crop"
},
"large": {
"w": 670,
"h": 728,
"resize": "fit"
},
"small": {
"w": 626,
"h": 680,
"resize": "fit"
},
"medium": {
"w": 670,
"h": 728,
"resize": "fit"
}
}
}]
},
"source": "\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e",
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 851119985732800513,
"id_str": "851119985732800513",
"name": "David",
"screen_name": "DavidHoperz",
"location": "",
"description": "",
"url": null,
"entities": {
"description": {
"urls": []
}
},
"protected": false,
"followers_count": 0,
"friends_count": 21,
"listed_count": 0,
"created_at": "Sun Apr 09 17:09:52 +0000 2017",
"favourites_count": 0,
"utc_offset": null,
"time_zone": null,
"geo_enabled": false,
"verified": false,
"statuses_count": 3,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "F5F8FA",
"profile_background_image_url": null,
"profile_background_image_url_https": null,
"profile_background_tile": false,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
"profile_link_color": "1DA1F2",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": true,
"default_profile_image": false,
"following": false,
"follow_request_sent": false,
"notifications": false,
"translator_type": "none"
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"is_quote_status": false,
"retweet_count": 0,
"favorite_count": 0,
"favorited": false,
"retweeted": false,
"possibly_sensitive": false,
"lang": "en"
}, {
"created_at": "Sun Apr 09 17:47:36 +0000 2017",
"id": 851129482832470026,
"id_str": "851129482832470026",
"text": "How to display twitter posts using javascript https:\/\/t.co\/I49vjVbAUJ",
"truncated": false,
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": [{
"url": "https:\/\/t.co\/I49vjVbAUJ",
"expanded_url": "https:\/\/www.script-tutorials.com\/how-to-display-twitter-posts-using-javascript\/",
"display_url": "script-tutorials.com\/how-to-display\u2026",
"indices": [46, 69]
}]
},
"source": "\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e",
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 851119985732800513,
"id_str": "851119985732800513",
"name": "David",
"screen_name": "DavidHoperz",
"location": "",
"description": "",
"url": null,
"entities": {
"description": {
"urls": []
}
},
"protected": false,
"followers_count": 0,
"friends_count": 21,
"listed_count": 0,
"created_at": "Sun Apr 09 17:09:52 +0000 2017",
"favourites_count": 0,
"utc_offset": null,
"time_zone": null,
"geo_enabled": false,
"verified": false,
"statuses_count": 3,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "F5F8FA",
"profile_background_image_url": null,
"profile_background_image_url_https": null,
"profile_background_tile": false,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
"profile_link_color": "1DA1F2",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": true,
"default_profile_image": false,
"following": false,
"follow_request_sent": false,
"notifications": false,
"translator_type": "none"
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"is_quote_status": false,
"retweet_count": 2,
"favorite_count": 1,
"favorited": false,
"retweeted": false,
"possibly_sensitive": false,
"lang": "en"
}, {
"created_at": "Sun Apr 09 17:33:43 +0000 2017",
"id": 851125986976567297,
"id_str": "851125986976567297",
"text": "Hello world",
"truncated": false,
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": []
},
"source": "\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e",
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 851119985732800513,
"id_str": "851119985732800513",
"name": "David",
"screen_name": "DavidHoperz",
"location": "",
"description": "",
"url": null,
"entities": {
"description": {
"urls": []
}
},
"protected": false,
"followers_count": 0,
"friends_count": 21,
"listed_count": 0,
"created_at": "Sun Apr 09 17:09:52 +0000 2017",
"favourites_count": 0,
"utc_offset": null,
"time_zone": null,
"geo_enabled": false,
"verified": false,
"statuses_count": 3,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "F5F8FA",
"profile_background_image_url": null,
"profile_background_image_url_https": null,
"profile_background_tile": false,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
"profile_link_color": "1DA1F2",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": true,
"default_profile_image": false,
"following": false,
"follow_request_sent": false,
"notifications": false,
"translator_type": "none"
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"is_quote_status": false,
"retweet_count": 0,
"favorite_count": 0,
"favorited": false,
"retweeted": false,
"lang": "en"
}]
Now, I'm trying to use jQuery and AJAX/jsonp to read the data from that PHP page and display the twitter feed on my page.
So I did this:
var poutput = $('#divTweets');
$.ajax({
url: 'config.php',
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
success: function(data, status){
$.each(data, function(pi,item){
str = item.id_str;
var products = '<div id="'+item.id_str+'" class="items">'+
'<p class="names">'+item.text+'</p>'+
'</div>';
poutput.append(products);
});
},
error: function(){
//alert('There was an error loading the data.');
}
});
However, when I run my code, I get nothing in my HTML Page. I did look in the console for errors and there was none.
Could someone please advice on this issue?
Get rid of jsonp: 'jsoncallback', and set dataType:'json' not 'jsonp'
You are returning json not jsonp.
Add error handling for further debugging
Let's say I have a JSON that looks like this:
[{
"created_at": "Sun Apr 09 17:56:20 +0000 2017",
"id": 851131679167139842,
"id_str": "851131679167139842",
"text": "hello world with an image goes here as a test! https:\/\/t.co\/jNfdESxPcn",
"truncated": false,
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": [],
"media": [{
"id": 851131665623732225,
"id_str": "851131665623732225",
"indices": [47, 70],
"media_url": "http:\/\/pbs.twimg.com\/media\/C8_TvYqW0AEbBaP.jpg",
"media_url_https": "https:\/\/pbs.twimg.com\/media\/C8_TvYqW0AEbBaP.jpg",
"url": "https:\/\/t.co\/jNfdESxPcn",
"display_url": "pic.twitter.com\/jNfdESxPcn",
"expanded_url": "https:\/\/twitter.com\/DavidHoperz\/status\/851131679167139842\/photo\/1",
"type": "photo",
"sizes": {
"thumb": {
"w": 150,
"h": 150,
"resize": "crop"
},
"large": {
"w": 670,
"h": 728,
"resize": "fit"
},
"small": {
"w": 626,
"h": 680,
"resize": "fit"
},
"medium": {
"w": 670,
"h": 728,
"resize": "fit"
}
}
}]
},
"extended_entities": {
"media": [{
"id": 851131665623732225,
"id_str": "851131665623732225",
"indices": [47, 70],
"media_url": "http:\/\/pbs.twimg.com\/media\/C8_TvYqW0AEbBaP.jpg",
"media_url_https": "https:\/\/pbs.twimg.com\/media\/C8_TvYqW0AEbBaP.jpg",
"url": "https:\/\/t.co\/jNfdESxPcn",
"display_url": "pic.twitter.com\/jNfdESxPcn",
"expanded_url": "https:\/\/twitter.com\/DavidHoperz\/status\/851131679167139842\/photo\/1",
"type": "photo",
"sizes": {
"thumb": {
"w": 150,
"h": 150,
"resize": "crop"
},
"large": {
"w": 670,
"h": 728,
"resize": "fit"
},
"small": {
"w": 626,
"h": 680,
"resize": "fit"
},
"medium": {
"w": 670,
"h": 728,
"resize": "fit"
}
}
}]
},
"source": "\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e",
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 851119985732800513,
"id_str": "851119985732800513",
"name": "David",
"screen_name": "DavidHoperz",
"location": "",
"description": "",
"url": null,
"entities": {
"description": {
"urls": []
}
},
"protected": false,
"followers_count": 0,
"friends_count": 21,
"listed_count": 0,
"created_at": "Sun Apr 09 17:09:52 +0000 2017",
"favourites_count": 0,
"utc_offset": null,
"time_zone": null,
"geo_enabled": false,
"verified": false,
"statuses_count": 3,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "F5F8FA",
"profile_background_image_url": null,
"profile_background_image_url_https": null,
"profile_background_tile": false,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
"profile_link_color": "1DA1F2",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": true,
"default_profile_image": false,
"following": false,
"follow_request_sent": false,
"notifications": false,
"translator_type": "none"
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"is_quote_status": false,
"retweet_count": 0,
"favorite_count": 0,
"favorited": false,
"retweeted": false,
"possibly_sensitive": false,
"lang": "en"
}, {
"created_at": "Sun Apr 09 17:47:36 +0000 2017",
"id": 851129482832470026,
"id_str": "851129482832470026",
"text": "How to display twitter posts using javascript https:\/\/t.co\/I49vjVbAUJ",
"truncated": false,
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": [{
"url": "https:\/\/t.co\/I49vjVbAUJ",
"expanded_url": "https:\/\/www.script-tutorials.com\/how-to-display-twitter-posts-using-javascript\/",
"display_url": "script-tutorials.com\/how-to-display\u2026",
"indices": [46, 69]
}]
},
"source": "\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e",
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 851119985732800513,
"id_str": "851119985732800513",
"name": "David",
"screen_name": "DavidHoperz",
"location": "",
"description": "",
"url": null,
"entities": {
"description": {
"urls": []
}
},
"protected": false,
"followers_count": 0,
"friends_count": 21,
"listed_count": 0,
"created_at": "Sun Apr 09 17:09:52 +0000 2017",
"favourites_count": 0,
"utc_offset": null,
"time_zone": null,
"geo_enabled": false,
"verified": false,
"statuses_count": 3,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "F5F8FA",
"profile_background_image_url": null,
"profile_background_image_url_https": null,
"profile_background_tile": false,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
"profile_link_color": "1DA1F2",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": true,
"default_profile_image": false,
"following": false,
"follow_request_sent": false,
"notifications": false,
"translator_type": "none"
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"is_quote_status": false,
"retweet_count": 2,
"favorite_count": 1,
"favorited": false,
"retweeted": false,
"possibly_sensitive": false,
"lang": "en"
}, {
"created_at": "Sun Apr 09 17:33:43 +0000 2017",
"id": 851125986976567297,
"id_str": "851125986976567297",
"text": "Hello world",
"truncated": false,
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": []
},
"source": "\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e",
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 851119985732800513,
"id_str": "851119985732800513",
"name": "David",
"screen_name": "DavidHoperz",
"location": "",
"description": "",
"url": null,
"entities": {
"description": {
"urls": []
}
},
"protected": false,
"followers_count": 0,
"friends_count": 21,
"listed_count": 0,
"created_at": "Sun Apr 09 17:09:52 +0000 2017",
"favourites_count": 0,
"utc_offset": null,
"time_zone": null,
"geo_enabled": false,
"verified": false,
"statuses_count": 3,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "F5F8FA",
"profile_background_image_url": null,
"profile_background_image_url_https": null,
"profile_background_tile": false,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
"profile_link_color": "1DA1F2",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": true,
"default_profile_image": false,
"following": false,
"follow_request_sent": false,
"notifications": false,
"translator_type": "none"
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"is_quote_status": false,
"retweet_count": 0,
"favorite_count": 0,
"favorited": false,
"retweeted": false,
"lang": "en"
}]
And I have a jQuery JSON that looks like this:
var poutput = $('.listHolder');
$.ajax({
url: 'page.php',
dataType: 'json',
timeout: 5000,
success: function(data){
$.each(data, function(pi,item){
str = item.id_str;
media = "";
var mediaUrl = item.entities.media.media_url_https;
alert(mediaUrl);
var products = '<div id="'+item.id_str+'" class="items">'+
'<p class="names">'+item.created_at+'</p>'+
'<p class="names">'+item.text+'</p>'+
media +
'</div>';
console.log(products);
poutput.append(products);
});
},
error: function(){
//alert('There was an error loading the data.');
}
});
I can easily get the created_at, text, and the id_str from that JSON but when I comes to getting the media_url or anything else, I always get an undefined error.
In my code above, I tried this:
var mediaUrl = item.entities.media.media_url_https;
alert(mediaUrl);
And it didn't work (undefined error).
I also tried:
var mediaUrl = item.entities.media_url_https;
alert(mediaUrl);
And still no luck.
Could someone please advise on this issue?
Instead of this:
var mediaUrl = item.entities.media.media_url_https;
try this:
var mediaUrl = item.entities.media[0].media_url_https;
as media is an array and could contain more than 1 item, so you'll need to specify the index.
when I am testing my app with GET friends/list API of twitter the "following" key of the JSON always turn up to be false/null even if the user following me . Why does this scenario keep occuring?
Sample JSON
{
"users": [{
"id": 103550181,
"id_str": "103550181",
"name": "Crowdfire",
"screen_name": "Crowdfire",
"location": "Everywhere",
"description": "Publish, engage and grow better on Twitter and Instagram. Available on mobile and web. 13 million users.",
"url": "https:\/\/t.co\/MEjmtjz2An",
"entities": {
"url": {
"urls": [{
"url": "https:\/\/t.co\/MEjmtjz2An",
"expanded_url": "http:\/\/www.crowdfireapp.com",
"display_url": "crowdfireapp.com",
"indices": [0, 23]
}]
},
"description": {
"urls": []
}
},
"protected": false,
"followers_count": 4270829,
"friends_count": 202935,
"listed_count": 6121,
"created_at": "Sun Jan 10 11:47:21 +0000 2010",
"favourites_count": 6434,
"utc_offset": 19800,
"time_zone": "Mumbai",
"geo_enabled": true,
"verified": false,
"statuses_count": 33003,
"lang": "en",
"status": {
"created_at": "Thu Jan 14 17:20:38 +0000 2016",
"id": 687685769708711936,
"id_str": "687685769708711936",
"text": "#NaveenS16 would you like to name some of them? #cfchat",
"source": "\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e",
"truncated": false,
"in_reply_to_status_id": 687685529454809088,
"in_reply_to_status_id_str": "687685529454809088",
"in_reply_to_user_id": 749455278,
"in_reply_to_user_id_str": "749455278",
"in_reply_to_screen_name": "NaveenS16",
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"retweet_count": 25,
"favorite_count": 58,
"entities": {
"hashtags": [{
"text": "cfchat",
"indices": [48, 55]
}],
"symbols": [],
"user_mentions": [{
"screen_name": "NaveenS16",
"name": "Naveen-Themesfinity",
"id": 749455278,
"id_str": "749455278",
"indices": [0, 10]
}],
"urls": []
},
"favorited": false,
"retweeted": false,
"lang": "en"
},
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "0099B9",
"profile_background_image_url": "http:\/\/abs.twimg.com\/images\/themes\/theme4\/bg.gif",
"profile_background_image_url_https": "https:\/\/abs.twimg.com\/images\/themes\/theme4\/bg.gif",
"profile_background_tile": false,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/615941584706650112\/nYaFiaed_normal.png",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/615941584706650112\/nYaFiaed_normal.png",
"profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/103550181\/1450267030",
"profile_link_color": "0099B9",
"profile_sidebar_border_color": "5ED4DC",
"profile_sidebar_fill_color": "95E8EC",
"profile_text_color": "3C3940",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": false,
"default_profile_image": false,
"following": false,
"follow_request_sent": false,
"notifications": false,
"muting": false,
"blocking": false,
"blocked_by": false
}, {
"id": 4176844046,
"id_str": "4176844046",
"name": "Anamika Nayan",
"screen_name": "anamika_nayan",
"location": "",
"description": "",
"url": null,
"entities": {
"description": {
"urls": []
}
},
"protected": false,
"followers_count": 1,
"friends_count": 41,
"listed_count": 0,
"created_at": "Mon Nov 09 07:27:26 +0000 2015",
"favourites_count": 0,
"utc_offset": null,
"time_zone": null,
"geo_enabled": false,
"verified": false,
"statuses_count": 0,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "C0DEED",
"profile_background_image_url": "http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png",
"profile_background_image_url_https": "https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png",
"profile_background_tile": false,
"profile_image_url": "http:\/\/abs.twimg.com\/sticky\/default_profile_images\/default_profile_4_normal.png",
"profile_image_url_https": "https:\/\/abs.twimg.com\/sticky\/default_profile_images\/default_profile_4_normal.png",
"profile_link_color": "0084B4",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": true,
"default_profile_image": true,
"following": false,
"follow_request_sent": false,
"notifications": false,
"muting": false,
"blocking": false,
"blocked_by": false
}, {
"id": 29921858,
"id_str": "29921858",
"name": "Sandip Saha",
"screen_name": "withsandip",
"location": "",
"description": "Technology geek | Co-founder # Innofied Solution Pvt Ltd | Husband to a lovely wife.",
"url": "http:\/\/t.co\/y7Swu7hNK6",
"entities": {
"url": {
"urls": [{
"url": "http:\/\/t.co\/y7Swu7hNK6",
"expanded_url": "http:\/\/www.sandipsaha.com",
"display_url": "sandipsaha.com",
"indices": [0, 22]
}]
},
"description": {
"urls": []
}
},
"protected": false,
"followers_count": 161,
"friends_count": 288,
"listed_count": 3,
"created_at": "Thu Apr 09 04:36:46 +0000 2009",
"favourites_count": 2,
"utc_offset": null,
"time_zone": null,
"geo_enabled": false,
"verified": false,
"statuses_count": 75,
"lang": "en",
"status": {
"created_at": "Fri Jan 08 04:21:37 +0000 2016",
"id": 685315393624248320,
"id_str": "685315393624248320",
"text": "RT #innofied: 16 New Year Resolutions to Welcome 2016 in Innofied - https:\/\/t.co\/6dUq8h6WmZ via #innofied",
"source": "\u003ca href=\"http:\/\/twitter.com\/download\/android\" rel=\"nofollow\"\u003eTwitter for Android\u003c\/a\u003e",
"truncated": false,
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"retweeted_status": {
"created_at": "Thu Jan 07 12:08:24 +0000 2016",
"id": 685070477706919936,
"id_str": "685070477706919936",
"text": "16 New Year Resolutions to Welcome 2016 in Innofied - https:\/\/t.co\/6dUq8h6WmZ via #innofied",
"source": "\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e",
"truncated": false,
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"retweet_count": 4,
"favorite_count": 1,
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [{
"screen_name": "innofied",
"name": "Innofied",
"id": 461027581,
"id_str": "461027581",
"indices": [82, 91]
}],
"urls": [{
"url": "https:\/\/t.co\/6dUq8h6WmZ",
"expanded_url": "http:\/\/goo.gl\/lmRKJK",
"display_url": "goo.gl\/lmRKJK",
"indices": [54, 77]
}]
},
"favorited": false,
"retweeted": false,
"possibly_sensitive": false,
"lang": "en"
},
"retweet_count": 4,
"favorite_count": 0,
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [{
"screen_name": "innofied",
"name": "Innofied",
"id": 461027581,
"id_str": "461027581",
"indices": [3, 12]
}, {
"screen_name": "innofied",
"name": "Innofied",
"id": 461027581,
"id_str": "461027581",
"indices": [96, 105]
}],
"urls": [{
"url": "https:\/\/t.co\/6dUq8h6WmZ",
"expanded_url": "http:\/\/goo.gl\/lmRKJK",
"display_url": "goo.gl\/lmRKJK",
"indices": [68, 91]
}]
},
"favorited": false,
"retweeted": false,
"possibly_sensitive": false,
"lang": "en"
},
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "C0DEED",
"profile_background_image_url": "http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png",
"profile_background_image_url_https": "https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png",
"profile_background_tile": false,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/603224208\/san_face_normal.jpg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/603224208\/san_face_normal.jpg",
"profile_link_color": "0084B4",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": true,
"default_profile_image": false,
"following": false,
"follow_request_sent": false,
"notifications": false,
"muting": false,
"blocking": false,
"blocked_by": false
}, {
"id": 281135790,
"id_str": "281135790",
"name": "manoj bose",
"screen_name": "manoj_bose",
"location": "",
"description": "",
"url": null,
"entities": {
"description": {
"urls": []
}
},
"protected": false,
"followers_count": 3,
"friends_count": 10,
"listed_count": 0,
"created_at": "Tue Apr 12 18:17:41 +0000 2011",
"favourites_count": 0,
"utc_offset": null,
"time_zone": null,
"geo_enabled": false,
"verified": false,
"statuses_count": 2,
"lang": "en",
"status": {
"created_at": "Tue Nov 06 18:22:31 +0000 2012",
"id": 265881866744254464,
"id_str": "265881866744254464",
"text": "Hello",
"source": "\u003ca href=\"http:\/\/www.nimbuzz.com\" rel=\"nofollow\"\u003eNimbuzz Mobile\u003c\/a\u003e",
"truncated": false,
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"retweet_count": 0,
"favorite_count": 0,
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": []
},
"favorited": false,
"retweeted": false,
"lang": "en"
},
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "C0DEED",
"profile_background_image_url": "http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png",
"profile_background_image_url_https": "https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png",
"profile_background_tile": false,
"profile_image_url": "http:\/\/abs.twimg.com\/sticky\/default_profile_images\/default_profile_5_normal.png",
"profile_image_url_https": "https:\/\/abs.twimg.com\/sticky\/default_profile_images\/default_profile_5_normal.png",
"profile_link_color": "0084B4",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": true,
"default_profile_image": true,
"following": false,
"follow_request_sent": false,
"notifications": false,
"muting": false,
"blocking": false,
"blocked_by": false
}, {
"id": 3187734769,
"id_str": "3187734769",
"name": "Parul",
"screen_name": "ParulGarg819",
"location": "",
"description": "",
"url": null,
"entities": {
"description": {
"urls": []
}
},
"protected": false,
"followers_count": 6,
"friends_count": 55,
"listed_count": 0,
"created_at": "Thu May 07 13:03:00 +0000 2015",
"favourites_count": 0,
"utc_offset": null,
"time_zone": null,
"geo_enabled": false,
"verified": false,
"statuses_count": 0,
"lang": "en-gb",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "C0DEED",
"profile_background_image_url": "http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png",
"profile_background_image_url_https": "https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png",
"profile_background_tile ": false,
"profile_image_url ": "http: \ /\/pbs.twimg.com\/profile_images\/596302789027868672\/wudduWN7_normal.jpg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/596302789027868672\/wudduWN7_normal.jpg",
"profile_link_color": "0084B4",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": true,
"default_profile_image": false,
"following": false,
"follow_request_sent": false,
"notifications": false,
"muting": false,
"blocking": false,
"blocked_by": false
}],
"next_cursor": 1523322916963527611,
"next_cursor_str": "1523322916963527611",
"previous_cursor": 0,
"previous_cursor_str": "0"
}
If you request it for a different user than the user making the authenticated request, then it may have a lot of false values as its follow status is based on the authenticated user.
https://api.twitter.com/1.1/friends/list.json?cursor=-1&screen_name=twitterapi&skip_status=true&include_user_entities=false
So for a generic account like twitterapi, most likely you will have a lot of false values, while for a screen_name that follows similar accounts you will have true values.
Try without specifying screen_name and you should always have true values.