JSON in JavaSscript - Accessing fields and manipulations. ADD REMOVE, EDIT JSON ELEMENTS



ADD, REMOVE, EDIT JSON ELEMENTS 

Reference from https://techtothepoint.blogspot.com/

Demo Link: https://jsfiddle.net/axkdwku6/51/


// Sample json object

  var json ={
    "id": "",
    "entityname": "eset",
    "entitydescription": "",
    "entityTags": [],
    "esetname": "APAIBOT",
    "entityacl": [
        "public"
    ],     
    "fields": {
        "esetname": {
            "name": "esetname",
            "type": "string",
            "default": "",
            "id": "esetname",
            "field_order": 0,
            "entityreference": "",
            "acl": {
                "required": true
            }
        },
        "description": {
            "name": "description",
            "type": "string",
            "default": "",
            "id": "description",
            "field_order": 1,
            "entityreference": "",
            "acl": {
                "required": true
            }
        }  
    }
};


(var fld) json.fields =

{ "esetname": { "name": "esetname", "type": "string", "default": "", "id": "esetname", "field_order": 0, "entityreference": "", "acl": { "required": true } }, "description": { "name": "description", "type": "string", "default": "", "id": "description", "field_order": 1, "entityreference": "", "acl": { "required": true } } }

Object.keys(fld) -> esetname, description

Object.keys(fld)[0] -> esetname

fld.esetname = { "name": "esetname", "type": "string", "default": "", "id": "esetname", "field_order": 0, "entityreference": "", "acl": { "required": true } }

fld.esetname.type = "string"

fld.esetname['type'] = "string"

Passing  JSON Object as Querystring for REST API Call:


Some frameworks do not work with {obj} as querystring, even single object t need to be sent as  an array with enclosed by array bracket [], like [{obj}]

POST http://localhost:2404/address
Content-Type: application/json

[ {
"address1": "add12",
"address2": "add12",
"city": "city12",
"zip": "1212121",
"state": "ca"
}]

ADD JSON ELEMENTS

 // start with empty object.
 let branches={};
let address= {
"address1": "add12",
"address2": "add12",
"city": "city12",
"zip": "1212121",
"state": "ca"
}
let geocode = {
"lat": "37.774095",
"long": "-122.44708"
}
 
First add geocode to the address:
address.geocode = geocode ;
branches.address = address;
This above order works just fine adding elements .

Comments