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.
3 Answers 3
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; }
answered Aug 28, 2017 at 17:45
Hassan Imam
22.6k6 gold badges45 silver badges53 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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);
answered Aug 28, 2017 at 17:38
A l w a y s S u n n y
38.8k9 gold badges69 silver badges120 bronze badges
Comments
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:
answered Aug 28, 2017 at 16:57
Kunvar Singh
1,8852 gold badges16 silver badges21 bronze badges
4 Comments
738560
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>
Kunvar Singh
I have some slightly change, now you can try.
738560
this gives a compile time error - "missing name after . operator" - syntax error
738560
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]["$"]
lang-js
response.return[keyName][0]['$']beingresponsethe parsed object andkeyNamethe property you want to access.