I have trying to get collection of data from json object using map and assign key, pair reference in loop but its not working out for me. I can read value in iterations but I need to assign to key and value where I need help. its not recognising "key" and "value"
var j1 = _preDefineAnswerOptions.map(function(item){
return ["key": item.preDefineAnswerId, "value": item.text];
});
data-source structure
You need to return an object. Use {} around the key and value.
var j1 = _preDefineAnswerOptions.map(function(item){
return {"key": item.preDefineAnswerId, "value": item.text};
});
Related
I need to extract key and values from json to form different json output
Input Json
{"somekey":"xyz",
"properties":{
"key1":"value1",
"key2":"value2"
.....
}
Expected Output Json
{"somekey":"xyz",
"properties":{
"mainkey1": "value1"
"mainkey2": "value2"
....
}
}
I need to add "main" to the existing key and read all the keys. Consider there's n number of key value pairs in properties.
How can this be done dwl script?
I tried to map the objects using payload map and write manually the entire keys. As new key value pairs get added i had to write each keys manually into mapping.
You can use update operator to update the parent object. Update will keep the existing keys and only update the props object. To concatenate the property keys a mapObject can be used.
%dw 2.0
output application/json
---
payload update {
case props at .properties -> props mapObject ((value, key) -> {
("main" ++ (key as String)): value
})
}
I am trying to create simple code to fetch data from an api which contains the price of bitcoin and store it. I am new to json and have tried everything I could think of. Here is the code.
await getRemoteData('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd')
.then((response) => {
const data = JSON.parse(response);
const price = ${data.value};
})
Thanks in advance!
JSON looks something like {"foo": "bar"} so it's basically collection of strings and values. in order to get value from some json say value of key "price", in js you need to parse JSON string to JSON Object first (which you already did by const data = JSON.parse(response);) and get the price by:-
const price = data.price // assuming that the key is "price"
if you want some other value with different key, say key is "foo" then you can fetch the value by let valueOfFoo = data.foo
I am trying to pull multiple json keyvalue data within any given JSON object, from a JSON file produced via an API call, for only the JSON objects that contain a specific value within a keyvalue pair; this specific value within a keyvalue pair is obtained via iterating through a list. Hard to articulate, so let me show you:
Here is a list (fake list representing my real list) I have containing the "specific value within a keyvalue pair" I'm interested in:
mylist = ['device1', 'device2', 'device3']
And here is the json file (significantly abridged, obviously) that I am reading from my python script:
[
{
"name": "device12345",
"type": "other",
"os_name": "Microsoft Windows 10 Enterprise",
"os_version": null
},
{
"name": "device2",
"type": "other",
"os_name": "Linux",
"os_version": null
},
{
"name": "device67890",
"type": "virtual",
"os_name": "Microsoft Windows Server 2012 R2 Standard",
"os_version": null
}
]
I want to iterate through mylist, and for each item in mylist that matches the json keyvalue key "name" in the json file, print the values for 'name' and 'os_name'. In this case, I should see a result where after iterating through mylist, device2 matches the 'name' key in the json object that contains {'name':'device2'}, and then prints BOTH the 'name' value ('device2') and the 'os_name' value ('Linux'), and then continues iterating through mylist for the next value to iterate through the json file.
My code that does not work; let us assume the json file was opened correctly in python and is defined as my_json_file with type List:
for i in mylist:
for key in my_json_file:
if key == i:
deviceName = i.get('name')
deviceOS = i.get('os_name')
print(deviceName)
print(deviceOS)
I think the problem here is that I can't figure out how to "identify" json objects that "match" items from mylist, since the json objects are "flat" (i.e. 'device2' in my json file doesn't have 'name' and 'os_name' nested under it, which would allow me to dict.get('device2') and then retrieve nested data).
Sorry if I did not understand your question clearly, but it appears you're trying to read values off of the list instead of the JSON file since you're looking at i.get instead of key.get when key is what actually contains the information.
And for the optimization issue, I'd recommend converting your list of devices into a set. You can then iterate through the JSON array and check if a given name is in the set instead of doing it the other way. The advantage is that sets can return if they contain an item in O(1) time, meaning it will significantly speed up the overall speed of the program to O(n) where n = size of json array.
for key in my_json_file:
if key.get('name') in my_list:
deviceName = key.get('name')
deviceOS = key.get('os_name')
print(deviceName)
print(deviceOS)
var a={LogGroupName:'/sam/xxx/ab/xxx/ayb/lob-star',LogStreamName:'tsy98',S3_Bucket:'abc'};
var array=[a];
console.log(array);
[ { LogGroupName: '/sam/xxx/ab/xxx/ayb/lob-star',LogStreamName: 'tsy98',S3_Bucket: 'abc' } ]
Would require to dynamically find the length of the array and variablize the key value from the json object.
Output needed:
LogGroupName=/sam/xxx/ab/xxx/ayb/lob-star
console.log(LogGroupName);
Need help.
[
{
"type": "spline",
"name": "W dor\u0119czeniu",
"color": "rgba(128,179,236,1)",
"mystring": 599,
"data": ...
}
]
I am trying to access this json as json['W doręczeniu']['mysting'], and I get no value why is that?
You're trying to access the index "W doręczeniu" but that's not an index it's a value. Also, what you seem to have is an array of JSON objects.
The [ at the start marks the array, the first element of which is your JSON object. The JSON obj begins with the {
You're also trying to use a [ ], but JSON values are accessed with the dot operator.
I'm not sure which index you're actually trying to access, but try something like this:
var x = json[0].mystring;
The value of "W doręczeniu" is not a key, so you cannot use it to get a value. Since your json string is an array you'll have to do json[0].nameto access the first (and only) element in the array, which happens to be the object. Of course, this is assuming json is the variable you store the array into.
var json = [{"type":"spline","name":"W dor\u0119czeniu","color":"rgba(128,179,236,1)","mystring":599}];
console.log(json[0].mystring); //should give you what you want.
EDIT:
To get the last element in a js array, you can simply do this:
console.log( json[json.length -1].mystring ); // same output as the previous example
'length - 1' because js arrays are indexed at 0. There's probably a million and one ways to dynamically get the array element you want, which are out of the scope of this question.