Dynamic JSON structure - json

I am trying to make a JSON-key dynamic with the following data and function:
var jsonData = [ ];
addProcess("Text1", "Text2", jsonData)
function addProcessingPurpose(title, domain, arr) {
arr.push({
"First": title,
domain : {
"subitem": {
"Subsubitem": "text"
}
}
});
}
I get the desired output expect I don't seem to manage setting the keys dynamically from the function parameter. E.g. now it prints "domain" instead of "Text2" which is my desired output. How can this be solved please?

Sorry for the stupid question, solved it myself didn't saw I forgot the [ ] around the key.. Thank you anyway, maybe it can help someone else..

Related

Is there a way of getting a cleaner JSON out of Couchdb?

I am wondering if there is a way to get a cleaner JSON with just the docs out of Couchdb instead of getting it under "rows" and then "doc"
This is the default output
{
"total_rows":1,
"offset":0,
"rows":[
{
"id":"7d9fd5824f9029186c1eec1bda005e75",
"key":"7d9fd5824f9029186c1eec1bda005e75",
"value":{
"rev":"1-f99879f67cfd27685f92c884c236a0fd"
},
"doc":{
"_id":"7d9fd5824f9029186c1eec1bda005e75",
"_rev":"1-f99879f67cfd27685f92c884c236a0fd",
"title":"Hello World",
"messeges":"This is the first messege. Helloo there"
}
}
]
}
This is the desired output:
{
"_id":"7d9fd5824f9029186c1eec1bda005e75",
"_rev":"1-f99879f67cfd27685f92c884c236a0fd",
"title":"Hello World",
"messeges":"This is the first messege. Helloo there"
}
Thanks
It would be useful to see your code. I suspect this is the output of the alldocs api? If you know the ID of the document you want you can use the get api which returns the JSON you want. Otherwise you must loop through the "rows" ie
for (x in results.rows) {...}
and then use x.doc to get your JSON.
Thanks all for the help.
I figured out a way doing it in Node js by using a map function attached to there query
in the nano page they listed it as
alice.list().then((body) => {
body.rows.forEach((doc) => {
console.log(doc);
});
});
instead I used it like this
alice.list().then((body) => {
var result = body.rows.map( (x) => {
return x.doc
});
console.log(result);
});
which works for me.
Still new till couchdb and databases in general.

How to extract JSON path separately?

I'm new to the JSON and I'm wondering how to extract a path from this kind of URL- https://www.example.com/bio-chocolate-p952
separately this way: product - bio-chocolate; product_id: p952
All I was able to come up with is following JSON pattern, which extract the path together:
{
"v": 7,
"domain": "example.com",
"path.list": [
{
"$regex": "[a-z0-9-]+-[p0-9]{1,}",
"extract": "product"
}
]
}
Hopefully I made myself clear. Anyways thank you for your answers and time.
Have a nice day.
Michal
If you have pathString = 'bio-chocolate-p952' You can use pathString.split('-') which will return an array ['bio','chocolate','p952']

Reading json file in node.js

This must be something really silly. After spending the last few hours, I am here for help.
I have a users.json file
{
"Test_Session": {
"test_SessionID": [
{
"$": {
"id": "1"
},
"test_type": [
"1"
],
"Test_IDtest": [
"1"
],
"DataURL": [
"data1"
]
}
]
}
}
I try to read DataURL by
var jsonData = require('./users.json');
var test = JSON.stringify(jsonData)
console.log(test.Test_Session.test_SessionID.DataURL);
In console, I get "Can't read property test_SessionID of undefined".
What's going on?
Your main issue is that test_SessionID is an array, so when you try to access DataUrl, it will be undefined. You need to select the index of the test_SessionID object you want to read from. Try this:
console.log(test.Test_Session.test_SessionID[0].DataURL);
Also, you don't need to JSON.stringify anything, Node automatically reads the file in as JSON, so just doing
var jsonData = require('./users.json');
console.log(jsonData.Test_Session.test_SessionID[0].DataURL);
should work fine.
Node is already interpreting the JSON, try the following:
var test = require('./users.json');
console.log(test.Test_Session.test_SessionID[0].DataURL);

using JSON file to define array values in Node.js

In node.js my program app.js, i am defining array like this
var myList = [["SAHRUKH",47.49,"HIT"],["SALMAN",47.3,"FLOP"]];
console.log (myList)
It is giving output but i want an external JSON file to supply the parameter of myList array instead of me defining it hardcore
i have prepared a JSON file named ppm.json and change my code to
var myList = JSON.parse(fs.readFileSync('ppm.json', 'utf8'));
console.log (myList[1])
my ppm.json is this
{
"hero": "SAHRUKH",
"age": "47.49",
"lastpict": "HIT"
}
it giving me output as undefined in console. what is the problem. pls help.
Without more requirements it's hard to give a definitive answer, but one thing you can do:
app.js
var myList = require('./my_list.json');
console.log(myList);
my_list.json
[["SAHRUKH",47.49,"HIT"],["SALMAN",47.3,"FLOP"]]
You can use require() to load both JSON and JavaScript files.
For example,
myFile.json:
[["SAHRUKH",47.49,"HIT"],["SALMAN",47.3,"FLOP"]]
app.js:
var myList = require( './myFile.json' );
console.log (myList)
You're accessing your item wrong. You don't have an array you have an object.
Access your heros age like this:
myList['age']
You might also consider changing your file to look like this:
{
"SAHRUKH" : {
"age" : "47.49",
"lastpict" : "HIT"
}
}
In which case you'd get your hero's age like:
myList.SAHRUKH.age;
//Or Equivalently
myList['SAHRUKH']['age']; //The dot notation above is preferred though!
Or this
{ "heros" : [
{
"name" : "SAHRUKH",
"age" : "47.49",
"lastpict" : "HIT"
}
]}
In which case you'd get at age like:
myList.heros[0].age;
If you adjust your ppm.json file to look like:
[{
"hero": "SAHRUKH",
"age": "47.49",
"lastpict": "HIT"
}]
It should drop in and work directly. If you wanted to include multiple heroes, it would look like:
[
{
"hero": "SAHRUKH",
"age": "47.49",
"lastpict": "HIT"
},
{
"hero": "SALMAN",
"age": "47.3",
"lastpict": "FLOP"
}
]
Your resulting myList should be an array in the example you provided, with entry 0 being the first hero object (SAHRUKH) and 1 being the second, and so on.

Datatables process json response multiple objects

Im using Datatables Jquery plugin to do a bit of work, but i havent been able to understand how to process the Json response im getting from the server .
I get this response:
"aaData":[
[
{
"idTreatment":23,
"treatment":"Hospitalización",
"directions":"Directions",
"active":"1"
},
{
"idCIE10":1,
"cieCode":"A00",
"diagnostic":"COLERA",
"description":null,
"lastUsed":1386649580000,
"active":1
}
],
[
{
"idTreatment":27,
"treatment":"Antibiótico",
"directions":null,
"active":"1"
},
{
"idCIE10":1,
"cieCode":"A00",
"diagnostic":"COLERA",
"description":null,
"lastUsed":1386649580000,
"active":1
}
],
[
{
"idTreatment":33,
"treatment":"Hidratación Oral",
"directions":"Directions",
"active":"1"
},
{
"idCIE10":1,
"cieCode":"A00",
"diagnostic":"COLERA",
"description":null,
"lastUsed":1386649580000,
"active":1
}
]
]
}
I have been trying to use mData or mRender, but im not familiarized with datatables yet, so i would be really grateful if someone could thell me how can in print this in a table on html
thanks in advance
this javascript fucntion is the one im using to initialize the table, im trying to use aoColumns based on examples but im not sure if this is the best option; also based on example im using mData, to show just 2 columns for test, as you can see im trying to acces the "array" or the json response using the objects field names, but when the page is rendering theres a warning saying that it cant find the field.
function initializeTreatmentsTable(id){
treatmentsTable = $('#treatmentsTable').dataTable( {
"bSort":false,
"sAjaxSource":"./diagnosticTreatment",
"fnServerParams": function ( aoData ) { aoData.push( {name:"diagnosticId",value:id} ); },
"aoColumns": [
{ "mData": "idTreatment" },
{ "mData": "treatment" }
]
});
Well after looking several times over the API in the official website datatables.net i came to the conclusion that i had to test out.
If my problem isn't clear i will explain it again.
i have a table, and that table's data is obtained by a ajax request and a json response, the json response is a array of object but the objects themselves are composed of at least 2 objects ( {[[object1][object2]],[[object1][object2]]} ) so accessing them with mData was impossible at least for me, and the solution was so simple that i wonder i never tried, i just had to use mRender and a function.
function initializeTreatmentsTable(id){
treatmentsTable = $('#treatmentsTable').dataTable( {
"bSort":false,
"sAjaxSource":"./diagnosticTreatment",
"fnServerParams": function ( aoData ) { aoData.push( {name:"diagnosticId",value:id} ); },
"aoColumns": [
{ "bVisible": false,
"mRender": function(data,type,full){
return data.idTreatment;}
},
{ "mRender": function(data,type,full){
return full[0].treatment;
} }
]
});
The function above its the one im using to initialise the table for the first time, i got the problem that i just wanted 2 field from the first object and none from the second one, but i dont now how to do it, so i used the "full" parameter that contains the whole json response, and accesed it.
This might not be the best answer but it did the work for me, so i hope that if someone know how to improved it , feel free to do it, or so just comment , and i also hope this can help someone else.