I am converting XML message with attributes to JSON and assigning to a variable. Now variable x
has the converted JSON message. Now I want to extract the key value pairs from that variable x(string)
.
Please check the below details. XML Message is
<?xml version="1.0" encoding="UTF-8"?>
<OBJECT key="postorder" type="TEMP">
<TEMP>
<ROW order-version-id="1"
layout-type="2"
order-no="3"
order-action="4"
estimated-price="5"
orig-price="6"
quantity="7"
orig-quantity="8"
quantity-type="6"
trade-currency="7"
base-currency="8"
settlement-currency="9"/>
</TEMP>
</OBJECT>
and I am converting it using fast-xml-parser and the output is as below
{
"OBJECT":{
"key":"postorder",
"type":"TEMP",
"TEMP":{
"ROW":{
"order-version-id":"1",
"layout-type":"2",
"order-no":"3",
"order-action":"4",
"estimated-price":"5",
"orig-price":"6",
"quantity":"7",
"orig-quantity":"8",
"quantity-type":"6",
"trade-currency":"7",
"base-currency":"8",
"settlement-currency":"9"
}
}
}
}
and assigning it as var result = JSON.stringify(jsonObj);
now result of string type has the converted JSON message. From result, I need to get base-currency values
Any leads are appreciated.
Without further examples, I'm not sure if this is what you actually want, but as I understand:
You have this:
let x = '{k1: v1, k2: v2, k3: v3....}'
And you want to have this:
let object = {k1: v1, k2: v2,...}
The fastest way to do this would be using JSON.parse functionality:
let x = '{k1: v1, k2: v2, k3: v3....}';
let object = JSON.parse(x);
EDIT: In your specific example, you don't need to use JSON.stringify. You already have a JSON object, o access it like this:
let object = /*that parsed XML*/
let baseCurrency = object.OBJECT.TEMP.ROW['base-currency']; //this will give you the value as string
You can use JSON.parse
function to convert the string to JSON object.
Hope the below example would help you get started.
const x = '{"key1":"value1","key2":"value2"}'; const json = JSON.parse(x); console.log(json.key1); console.log(json.key2);
The result you get from the fast-xml-parser is a JSON object. You need not convert it into a string using JSON.stringify
. You can use that object directly and get the base-currency
value.
See the code below.
const jsonObj = { "OBJECT": { "key": "postorder", "type": "TEMP", "TEMP": { "ROW": { "order-version-id": "1", "layout-type": "2", "order-no": "3", "order-action": "4", "estimated-price": "5", "orig-price": "6", "quantity": "7", "orig-quantity": "8", "quantity-type": "6", "trade-currency": "7", "base-currency": "8", "settlement-currency": "9" } } } }; const baseCurrency = jsonObj.OBJECT.TEMP.ROW["base-currency"]; console.log(baseCurrency); // 8
First use JSON.parse
function to convert the string to JSON object.
const object1 =JSON.parse(jsonstr) ;
console.log(Object.keys(object1));
The Object.keys() method returns an array of a given object's own property names, in the same order as we get with a normal loop.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.