Convert JSON objects into"value" "value" pairs - json

I have a JSON object with this structure:
I am trying to get it into this format
{"123" : "Asthma", "124" : "Bronchitis".....}
I am pretty green with javascript and working with JSON. Some assistance would be great.
Thanks

Try something like this:
var arr = [{id: 1, name: 'a'},{id: 2, name: 'b'},{id: 3, name: 'c'}];
var obj = {};
for (var i=0 ; i<arr.length ; ++i) {
obj[arr[i].id] = arr[i].name;
}
console.log(obj);

Related

How to get value only from json array using Jquery?

I am trying to get the value only from a json array. I have search some answer in stackoverflow but seems like not the one for me.
my code looks like this:
.done(function (data) {
var jdata = JSON.stringify(data['queryBuilder']);
var arrdata = JSON.parse(jdata);
var fdata = JSON.stringify(arrdata);
printtopdf(fdata);
);
//this code is from the answer posted here in stackoverflow:
function printtopdf(fdata) {
var values = Object.keys(fdata).map(function (key) { return fdata[key]; });
console.log(fdata);
}
and the result:
[
{
"rownum":2,
"rr_id":"RR2100001",
"ref_id":"UCL2100001",
"cdescription":"65UGHDFH56Y</br>, 65UGHDFH56Y</br>",
"rr_status":"Pending",
"date_created":"2021-01-08 13:46:03"
}
]
I just want to get the value only, like this:
[
2,
"RR2100001",
"UCL2100001",
"65UGHDFH56Y</br>, 65UGHDFH56Y</br>",
"Pending",
"2021-01-08 13:46:03"
]
Any idea? thanks.
You can achieve this using Array.prototype.map and Object.prototype.values.
const data = [
{
"rownum":2,
"rr_id":"RR2100001",
"ref_id":"UCL2100001",
"cdescription":"65UGHDFH56Y</br>, 65UGHDFH56Y</br>",
"rr_status":"Pending",
"date_created":"2021-01-08 13:46:03"
}
];
// Get values for all data points
const v1 = data.map(value => Object.values(value));
console.log(v1);
// Get values for first data point
const v2 = Object.values(data.shift());
console.log(v2);

Parse json and give to category in Highcharts

I am using Highcharts and want to parse JSON and assign to categories part.
This is my chart data:
var chartData = {
"chart" :[
{name: "kolkata",categories: ["male", "female"]},
{name: "Mumbai",categories: ["male", "female"]}
]
}
I tried this:
var category = [];
var Count = chartData.chart;
for (var i=0; i< Count.length; i++) {
category.push({name : chartData.chart[i].name, categories : chartData.chart[i].categories})
}
Finally:
xAxis: {
categories: category
}
The category not coming properly on x axis. Does anybody have an idea why it's not?
For this you need to include library
include following library
<script src="http://blacklabel.github.io/grouped_categories/grouped-categories.js"></script>

How to remove {"Result": in JSON string?

I have the following JSON result when calling an API, how can I convert the JSON string I have so I can just see the results:
var response = request_.responseText;
var obj = JSON.parse(request_.response);
var j = obj;
var js = JSON.stringify(j)
console.log(js)
JSON Output of var js:
{"Result":[{"PK_ID":1,"MedicationId":1,"NHS_Number":"123","Medication_Name":"Asprin","Read_Code":"XaaYI","Dose":"500mg","Date_Started":"02/06/2016","Date_Ended":"03/06/2016"},{"PK_ID":2,"MedicationId":2,"NHS_Number":"1234","Medication_Name":"Ibuprofen","Read_Code":"EtQWEl","Dose":"100mg","Date_Started":"03/02/2016","Date_Ended":"05/02/2016"}]}
How can I remove the {"Result": } code that is wrapped around my JSON?
Update from
var j = obj;
to
var j = obj.Result;
var obj = {
"Result": [{
"PK_ID": 1,
"MedicationId": 1,
"NHS_Number": "123",
"Medication_Name": "Asprin",
"Read_Code": "XaaYI",
"Dose": "500mg",
"Date_Started": "02/06/2016",
"Date_Ended": "03/06/2016"
}, {
"PK_ID": 2,
"MedicationId": 2,
"NHS_Number": "1234",
"Medication_Name": "Ibuprofen",
"Read_Code": "EtQWEl",
"Dose": "100mg",
"Date_Started": "03/02/2016",
"Date_Ended": "05/02/2016"
}]
};
var j = obj.Result;
var js = JSON.stringify(j);
console.log(js);

Create dojox datagrid structure layout programmatically/dynamically

I want to create a dojo datagrid with a header looking like this :
As you can see I want some section headers (Section A, Section B...), containing many "subheaders" (A1, A2, A3... B1, B2...). I get all the data in a json response when I call the page. Then I was able to do two things :
First, get the data in the json to display all the subheaders, like this :
var gridStructure = [
{width:'150px', name:'Table'}
];
for(var i = 0 ; i < response.columns.length ; i++) {
for(var j = 0 ; j < response.columns[i].sections.length ; j++) {
var subColumnToAdd =
{width:'200px', name:response.columns[i].sections[j].sectionName};
gridStructure.push(subColumnToAdd);
}
}
grid.setStructure(gridStructure);
And I also was able to display a table like how I want, but not dynamically :
var gridStructure = [{
cells:[
[{width: 'auto'}],
[{
name: 'Section A',
colSpan: 2
}],
[{
name: 'A1',
field: 'col1'
}, {
name: 'A2',
field: 'col2'
}]
],
onBeforeRow : function(inDataIndex, inSubRows) {
inSubRows[0].invisible = true;
}
}];
grid.setStructure(gridStructure);
Now what I don't know to do is how to mix it, to fill the headers/subheaders with dynamic data. Thanks for your help and advices.
Try something like this:
var gridStructure = [
[
{width:'150px', name:'Table', rowSpan: 2}
]
];
for(var i = 0 ; i < response.columns.length ; i++) {
for(var j = 0 ; j < response.columns[i].sections.length ; j++) {
var subColumnToAdd =
{width:'200px', name:response.columns[i].sections[j].sectionName};
gridStructure.push(subColumnToAdd);
}
}
But that is adding the table statically... to make it completely dynamic, you'll still need something (perhaps in the data itself) that specifies the value for colSpan/rowSpan to get it to display properly.
To add the data, you just need to build a json array of your data. Each item of that array will be an object containing a single item. Should be straightforward if your data is already in this format, otherwise just create a loop and build the item dynamically.
var items = [
{ name: "First", someProperty: true},
{ name: "Second", someProperty: false}
];
Once you have it, there are many options on how to build stores:
store = new Memory({ data: items });
dataStore = new ObjectStore({ objectStore: store });
myGrid.set("store", dataStore);
or
grid = new DataGrid({
store: dataStore,
...
You can use this as a reference/example https://dojotoolkit.org/documentation/tutorials/1.9/datagrid/demo/datagrid-subrows.php

actionscript 3 JSON failed to parse data

I have received a JSON data like this:
'{"result":[[["test","test"],["test","test"]],[],[],[]]}'
OR
'{"result":[
[
["test","test"],
["test","test"]
],
[],
[],
[]
]
}'
But when I try to JSON.parse(data);, it is converted to object like this:
{[[["test","test"],["test","test"]],[],[],[]]:}
Is there anyway to fix it?
ADDITIONAL:
i have traced what happen before,during,after JSON do, and the problem seems to be the parse itself, sometime it work, some time doesn't
var object:Object = {test:[[["Test","test1"],["test2"]],["test3"],[],[]]}
var stringed:String = JSON.stringify(object);
trace(stringed)//{"test":[[["Test","test1"],["test2"]],["test3"],[],[]]}
var backed:Object = JSON.parse(stringed);
for each(var thigng:String in backed){
trace(thigng, "=", backed[thigng])//Test,test1,test2,test3,, = undefined
}
var object:Object = {"test":"test3"}
var stringed:String = JSON.stringify(object);
trace(stringed)//{test:"test3"}
var backed:Object = JSON.parse(stringed);
for each(var thigng:String in backed){
trace(thigng, "=", backed[thigng])//test3 = undefined
}
A "for each...in" loop will only give you the value not the key.
What you need is the for in loop.
As you can see from the example below where you went wrong
var object:Object = {"this_is_the_key":"VALUE"}
var stringed:String = JSON.stringify(object);
var backed:Object = JSON.parse(stringed);
for each(var thigng:String in backed){
trace('KEY:', thigng, ' VALUE:' ,backed[thigng]) // KEY: VALUE VALUE: undefined
}
trace('------')
for(thigng in backed){
trace('KEY:', thigng, ' VALUE:' ,backed[thigng]) //KEY: this_is_the_key VALUE: VALUE
}
Also this is not a valid JSON string
'{"result":[[["test","test"],["test","test"]],[],[],[]]}'