how to navigate through complex JSON dynamically in JavaScript -
i'm trying navigate through complex nested json, attempt didn't me far keeps returning me last index
json.
this how objects looks like, , trying navigate through , getting other objs/schemas in $ref
.
raw json
{ "type": "object", "properties": { "id": { "format": "int32", "type": "integer" }, "status": { "enum": [ "preparing", "awaitingcompletion", "cancelled", "completed" ], "type": "string" }, "externalreference": { "type": "string" }, "customer": { "$ref": "#/definitions/customer" }, "orderlinegroups": { "type": "array", "items": { "$ref": "#/definitions/orderlinegroup" } }, "promotions": { "type": "array", "items": { "$ref": "#/definitions/promotionsummary" } }, "originatingsite": { "type": "object", "properties": { "id": { "format": "int32", "type": "integer" }, "propertycode": { "type": "string" }, "storecode": { "type": "string" } } }, "customdata": { "type": "object", "additionalproperties": { "type": "array", "items": { "type": "object" } } } } }
in code have done for()
, hasownproperty()
, problem doesn't give me json condition met (e.g if there's no type property), gives last index
or object doesn't have type
property. doesn't return me of objects if type
property array
.
// property of #/definitions/obj let prop = apidefinition[splitresponse.split('/')[2]].properties; console.log([prop]) var s = [apidefinition[splitresponse.split('/')[2]].properties]; // transform js object of #/definitions/obj json var parentjson = json.stringify(apidefinition[splitresponse.split('/')[2]]); (var x in prop) { if (prop.hasownproperty(x)) { if (prop[x].type && prop[x].type === 'array') { console.log('all type array >> ', x); let objkeyprop = apidefinition[prop[x].items.$ref.split('/')[2]]; let objjsonstringified = json.stringify(objkeyprop); let refstring = '{"$ref"'+':' + '"' + prop[x].items.$ref + '"}'; this.compiledjson = json.parse(parentjson.replace(refstring, objjsonstringified)); } else if (!prop[x].type) { console.log('all arrays >> ', x) let objkeyprop = apidefinition[prop[x].$ref.split('/')[2]]; let objjsonstringified = json.stringify(objkeyprop); let refstring = '{"$ref"'+':' + '"' + prop[x].$ref + '"}'; this.compiledjson = json.parse(parentjson.replace(refstring, objjsonstringified)); } } }
Comments
Post a Comment