kavinhuh
kavinhuh

Reputation: 739

How to approach this json structure

I used some online link to convert an XML format data to JSON ,the result I got is

   {
  "SOAP-ENV:Envelope": {
    "-xmlns:SOAP-ENV": "http://schemas.xmlsoap.org/soap/envelope/",
    "-xmlns:SOAP-ENC": "http://schemas.xmlsoap.org/soap/encoding/",
    "-xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
    "-xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
    "-xmlns:hdr": "urn:PASAHeader",
    "-xmlns:ns": "urn:PSFVUC_DMTR",
    "-xmlns:ex": "urn:PASAexploitationWS",
    "SOAP-ENV:Body": {
      "-SOAP-ENV:encodingStyle": "http://schemas.xmlsoap.org/soap/encoding/",
      "-id": "_0",
      "ns:getEleLocTopicDtlResp": {
        "tControle": {
          "-xsi:type": "ns:tControle",
          "iErrorType": {
            "-xsi:type": "xsd:int",
            "#text": "0"
          },
          "iRetError": {
            "-xsi:type": "xsd:int",
            "#text": "0"
          },
          "sErrorMessage": { "-xsi:type": "xsd:string" }
        },
        "tGlobalSetting": {
          "-xsi:type": "ns:tGlobalSettingTopic",
          "iGlobalSettingId": {
            "-xsi:type": "xsd:int",
            "#text": "10"
          },
          "sGlobalSettingCode": {
            "-xsi:type": "xsd:string",
            "#text": "RAJA"
          },
          "iDisplayMode": {
            "-xsi:type": "xsd:int",
            "#text": "0"
          },
          "iOrderVariant": {
            "-xsi:type": "xsd:int",
            "#text": "0"
          }
        },
        "pListTopic": {
          "-xsi:type": "ns:sListELTopic",
          "nTopic": {
            "-xsi:type": "xsd:int",
            "#text": "2"
          },
          "pTListTopic": {
            "-xsi:type": "SOAP-ENC:Array",
            "-SOAP-ENC:offset": "[0]",
            "-SOAP-ENC:arrayType": "ns:tELTopic[2]",
            "item": [
              {
                "-xsi:type": "ns:tELTopic",
                "iTopicId": {
                  "-xsi:type": "xsd:int",
                  "#text": "5"
                },
                "sTopicName": {
                  "-xsi:type": "xsd:string",
                  "#text": "TOP1 TESTING PLEASEDONTDELETE"
                },
                "pListClearCriteria": {
                  "-xsi:type": "ns:sListClearCriteria",
                  "nClearCriteria": {
                    "-xsi:type": "xsd:int",
                    "#text": "0"
                  }
                },
                "pListImage": {
                  "-xsi:type": "ns:sListImage",
                  "nImage": {
                    "-xsi:type": "xsd:int",
                    "#text": "0"
                  }
                },
                "pListTag": {
                  "-xsi:type": "ns:sListELTag",
                  "nTag": {
                    "-xsi:type": "xsd:int",
                    "#text": "0"
                  }
                }
              },
              {
                "-xsi:type": "ns:tELTopic",
                "-SOAP-ENC:position": "[1]",
                "iTopicId": {
                  "-xsi:type": "xsd:int",
                  "#text": "6"
                },
                "sTopicName": {
                  "-xsi:type": "xsd:string",
                  "#text": "TOP2 TESTING PLEASEDONTDELETE"
                },
                "pListClearCriteria": {
                  "-xsi:type": "ns:sListClearCriteria",
                  "nClearCriteria": {
                    "-xsi:type": "xsd:int",
                    "#text": "0"
                  }
                },
                "pListImage": {
                  "-xsi:type": "ns:sListImage",
                  "nImage": {
                    "-xsi:type": "xsd:int",
                    "#text": "0"
                  }
                },
                "pListTag": {
                  "-xsi:type": "ns:sListELTag",
                  "nTag": {
                    "-xsi:type": "xsd:int",
                    "#text": "0"
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
}

Now I would like to add and append values dynamically in JS,but I have never come across such complicated JSON structure, I have come across JSON with key-value pairs.

The requirement is to convert this JSON (after alteration) to XML and send to server. How can we approach such JSON structure.Also is there any issues in using such JSON structures. updated with exact json I am to work with.

Scenario:say I have to append "pListTopic" item how can I manage the "-xsi:type": "SOAP-ENC:Array"

dynamically for each items my aim is to just add a new element to the array but I still have to maintain the "-xsi:type": values for each elements ,how can I achieve this.

Upvotes: 0

Views: 147

Answers (1)

cauchi
cauchi

Reputation: 1543

This is normal JSON. You append and modify values the usual way. For example, this code

var test={"first":{"first1":"blabla","first2":"blabla2"},"second":"blabla3","third":{"third1":"blabla4","third2":"blabla5"}}
console.log(test)
console.log('-----')
console.log(test.first)
console.log('-----')
console.log(test.first.first1)
console.log('-----')
test.fourth = "just added";
test.third.third2 = "just corrected";
test.third.third3 = "hey it works";
console.log(test)

prints on screen (using nodejs) the following:

{ first: { first1: 'blabla', first2: 'blabla2' },
  second: 'blabla3',
  third: { third1: 'blabla4', third2: 'blabla5' } }
-----
{ first1: 'blabla', first2: 'blabla2' }
-----
blabla
-----
{ first: { first1: 'blabla', first2: 'blabla2' },
  second: 'blabla3',
  third: 
   { third1: 'blabla4',
     third2: 'just corrected',
     third3: 'hey it works' },
  fourth: 'just added' }

which answers your question. Nevertheless, converting that object to JSON to only modify it and then change it to XML doesn't make much sense. I would modify the original XML structure.

Upvotes: 1

Related Questions