1

I am facing issue in converting json response in a required format. Request:

{
   "firstName": "ABC",
   "middleName": "V",
   "AddrCity": "CITY",
   "AddressLine2": "ADDRESS LINE 2",
   "LastName": "LASTNAME",
   "AddressLine1": "ADDR LINE1",
   "Country": "India",
   "customerId": "1234",
   "AddrPinCode": "999999"
}

Below is the response i am getting Response:

{"return": 
 {
   "response": [{"$": 1234}],
   "responseMessage": [{"$": "Success ABC"}],
   "responseCode": [{"$": "CITY,India"}]
 }
}

Notice the "$" symbol, which is giving problem while fetching the response. Below is the expected response and also need to fetch response, responseMessage & responseCode values accordingly

{"return": 
 {
   "response": 1234,
   "responseMessage": "Success ABC",
   "responseCode": "CITY,India"
 }
}

Thanks for your quick response in advance.

7
  • 2
    Please format the code properly - this is not readable right now. Commented Aug 28, 2017 at 16:21
  • what do you want? Commented Aug 28, 2017 at 16:24
  • Can you please slowly explain the thing? It is hard to understand Commented Aug 28, 2017 at 16:27
  • When i getting the response, json object contains "$", with which i am not able to fetch the values using obj.result.response as "1234". Commented Aug 28, 2017 at 16:32
  • 1
    The response format is not perfect, but it's valid, and you can access the properties like this: response.return[keyName][0]['$'] being response the parsed object and keyName the property you want to access. Commented Aug 28, 2017 at 16:34

3 Answers 3

1

You can access the value inside the JSON using bracket notation.

var str = '{"return": {"response": [{"$": 1234}],"responseMessage": [{"$": "Success ABC"}],"responseCode": [{"$": "CITY,India"}]}}';

var obj = JSON.parse(str);
console.log(obj.return.response[0]['$']);
console.log(obj.return.responseMessage[0]['$']);
console.log(obj.return.responseCode[0]['$']);
.as-console-wrapper { max-height: 100% !important; top: 0; }

You can use array#foreach and Object.key() to get the desired object.

var str = '{"return": {"response": [{"$": 1234}],"responseMessage": [{"$": "Success ABC"}],"responseCode": [{"$": "CITY,India"}]}}';

var obj = JSON.parse(str);

Object.keys(obj.return).forEach((key) => {
  obj.return[key] = obj.return[key][0]['$'];
});

console.log(obj)
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

0

You can try this two ways

Way 1:(preferred)

        let str = `{"return":
                     {
                       "response": [{"$": 1234}],
                       "responseMessage": [{"$": "Success ABC"}],
                       "responseCode": [{"$": "CITY,India"}]
                     }
                    }`;
    
    
        let js_object = JSON.parse(str); // parse json string to javascript object
        let js_object_return = js_object.return;
        let formated_obj = {};
        let desired_obj = {};
        Object.keys(js_object_return).forEach(function(key) {
            formated_obj[key] = js_object_return[key][0]["$"];
        });
        desired_obj['return']=formated_obj;
        console.log(desired_obj.return.response);

Way 2:

let regex = /\[{"\$": ("?([a-z0-9A-Z\s,]+"?))}\]/gm;
        let str = `{"return":
                     {
                       "response": [{"\$": 1234}],
                       "responseMessage": [{"\$": "Success ABC"}],
                       "responseCode": [{"\$": "CITY,India"}]
                     }
                    }`;
        let  subst = `$1`;
    
        // The substituted value will be contained in the result variable
        let result = str.replace(regex, subst);
        let desired_object = JSON.parse(result); // parse json string to javascript object
        
        console.log(desired_object.return.response);

Comments

0
var obj={"return": 
 {
   "response": [{"$": 1234}],
   "responseMessage": [{"$": "Success ABC"}],
   "responseCode": [{"$": "CITY,India"}]
 }
};

obj.return.response[0].['$'];
obj.return.responseMessage[0].['$'];
obj.return.responseCode[0].['$'];

Try out this solution:

4 Comments

tried this too, its failing with same error <pre><exception class="org.mozilla.javascript.EcmaError"> TypeError: Cannot read property "$" from undefined (JScript#102) <f>com.collaxa.cube.engine.rhino.JSContextFactory.doTopCall#109</f> <f>org.mozilla.javascript.ScriptRuntime.doTopCall#3091</f> <f>com.collaxa.cube.engine.rhino.JS.exec#279</f> </exception> </pre>
I have some slightly change, now you can try.
this gives a compile time error - "missing name after . operator" - syntax error
Thanks All for the support, i did certain other changes to the code and the above suggested code snippet worked. <br>obj.return["response"][0]["$"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.