Trying to print the Nested Json - javascript

var key = " ";
var myBio = {
"name":"Sathya",
"age":"23",
"position":"Soft.Engineer",
"email":
{
"email1":"sathya#gmail.com",
"email2":"sathya#knstek.com"},
};
for (key in myBio){
var y = myBio[key];
console.log(key+" : "+ y);
}
output:
name : Sathya
age : 23
position : Soft.Engineer
email : [object Object]
Required Output:
name : Sathya
age : 23
position : Soft.Engineer
email :
email1:sathya#gmail.com
email2:sathya#knstek.com
I can print this Emails separately using another loop. But I want to print with main loop only . Any ways to do this using JavaScript??

You could use a recursive function to get this done.. example
function logRecursive(object){
for (key in object){
var value=object[key];
if(typeof value === 'object'){
console.log('{');
logRecursive(value)
console.log('}');
}else{
console.log(value);
}
}

function recursion(myBio) {
for (var key in myBio) {
if (typeof(myBio[key]) == 'object') {
recursion(myBio[key]);
} else {
alert("Key: " + key + " Values: " + myBio[key]);
}
}
}
use this subroutine if you have nested json

Try this:
var myBio = {
"name" : "Sathya",
"age" : "23",
"position" : "Soft.Engineer",
"email" : {
"email1" : "sathya#gmail.com",
"email2" : "sathya#knstek.com"
},
};
function print(bio) {
for (var key in bio) {
if (typeof(bio[key]) == 'object') {
print(bio[key]);
} else {
console.log(key + ": " + bio[key]);
}
}
}
print(myBio);

Most people searching in Google and finding this question are probably looking for something like this:
console.log(JSON.stringify(object, null, 2));
This prints deeply nested JSON to the console without the annoying "[Object]".
The last two parameters are optional. The 2 says to pretty print the JSON with an indent of 2. If left blank, will output non-formatted JSON.
This does not answer this specific question, but hopefully will answer those searching for "Print nested JSON Javascript".

Try this :
var myBio = {
"name":"Sathya",
"age":"23",
"position":"Soft.Engineer",
"email": {
"email1":"sathya#gmail.com",
"email2":"sathya#knstek.com"
}
};
var result = parseJSON(myBio);
console.log(result);
function parseJSON(obj) {
var parsedData = '';
for(var i in obj) {
if (typeof obj[i] == 'object') {
parsedData += parseJSON(obj[i]);
}else {
parsedData += i +' : '+ obj[i];
}//end if
parsedData += '\n';
}//end for
return parsedData;
}//end function

Related

Check for special characters in every JSON field [duplicate]

I have an object which has inner objects and properties defined like this:
var obj = {obj1 : { "prop1" : "nothing", "prop2" : "prop"},
obj2 : {"prop1" : "nothing", "prop2" : "prop"},
pr1 : "message",
pr2 : "mess"
};
Normally to iterate every property of an object , the for .. in loop can do the trick
for (property in obj){
if (obj.hasOwnProperty(property)){
console.log(property + " " + obj[property]);
}
}
the console displayed :
obj1 [object Object]
obj12 [object Object]
pr1 message
pr2 mess
However how to iterate the inner objects (obj1, obj2) and their own properties (prop1,prop2) ?
Recursion is your friend:
function iterate(obj) {
for (var property in obj) {
if (obj.hasOwnProperty(property)) {
if (typeof obj[property] == "object")
iterate(obj[property]);
else
console.log(property + " " + obj[property]);
}
}
}
Note: don't forget to declare property locally using var!
That's great anwsers, although the array cases is not covered, here's my contribution:
var getProps = function (obj) {
for (var property in obj) {
if (obj.hasOwnProperty(property) && obj[property] != null) {
if (obj[property].constructor == Object) {
getProps(obj[property]);
} else if (obj[property].constructor == Array) {
for (var i = 0; i < obj[property].length; i++) {
getProps(obj[property][i]);
}
} else {
console.log(obj[property]);
}
}
}
}
getProps(myObject);
To simply display the object structure, I often use: console.log (JSON.stringify (obj))
You can use recursion to achieve that:
function Props(obj) {
function getProps(obj){
for (var property in obj) {
if (obj.hasOwnProperty(property)){
if (obj[property].constructor == Object) {
console.log('**Object -> '+property+': ');
getProps(obj[property]);
} else {
console.log(property + " " + obj[property]);
}
}
}
}
getProps(obj);
}
see http://jsfiddle.net/KooiInc/hg6dU/

show nested json data in treeview in javascript

i have nested json data. i used the blow function.
var jsonSource={"error_code":0, "ext_info":{"name":{"firstName":"John","lastName":"Jonson","nickName":"JJ"}}};
var obj=JSON.parse(jsonSource),returnValue;
function showJson(obj){
for(var key in obj){
if(typeof obj[key]==='object'){
returnValue+='<div>'+key+'/\n';
showJson(obj[key]);
returnValue+='</div>';
} else{
returnValue+=key+'equal'+obj[key];
}
}
docoument.getElementById('data').innerHTML=returnValue;
}
as i said before , i have a large nested json data and when i parse it to showJson function ,it just shows one level of json data and puts others deep level of dataJson undefined.
what should i do to resolve the problem?
Recursive approach works more intuitively when done with actual return values. Have a look at https://jsfiddle.net/ughnjfh0/1/
var jsonSource='{"error_code":0, "ext_info":{"name":{"firstName":"John","lastName":"Jonson","nickName":"JJ"}}}';
var obj=JSON.parse(jsonSource);
function showJson(obj){
var returnValue='';
for(var key in obj){
if(typeof obj[key]==='object'){
returnValue+='<div>'+key+'/\n';
returnValue+=showJson(obj[key]);
returnValue+='</div>';
} else{
returnValue+=key+'equal'+obj[key];
}
}
return returnValue;
}
document.getElementById('data').innerHTML= showJson(obj);
Also:
jsonSource should be a string to be properly parsable as JSON data
typo in docoument.getElementById('data').innerHTML=returnValue;
Some of your problems:
jsonSource is already an object
you try to assign the returnValue in every call of showJson
Better to use a clean approach for looping and returning of the items:
var obj = { "error_code": 0, "ext_info": { "name": { "firstName": "John", "lastName": "Jonson", "nickName": "JJ" } } };
function showObj(obj) {
return Object.keys(obj).map(function (k) {
if (typeof obj[k] === 'object') {
return k + ':<br><div style="margin-left: 25px;">' + showObj(obj[k]) + '</div>';
}
return k + ': ' + obj[k];
}).join('<br>');
}
document.getElementById('data').innerHTML = showObj(obj);
<div id="data"></div>
// obj is the object to loop, ul is the ul to append lis to
function loop(obj, ul) {
$.each(obj, function(key, val) {
if(val && typeof val === "object") { // object, call recursively
var ul2 = $("<ul>").appendTo(
$("<li>").appendTo(ul)
);
loop(val, ul2);
} else {
$("<li>").text(val).appendTo(ul);
}
});
}
var ul = $("<ul>");
var jsonSource={"error_code":0, "ext_info":{"name":{"firstName":"John","lastName":"Jonson","nickName":"JJ"}}};
var data=JSON.parse(jsonSource)
loop(data, ul);
ul.addClass("my-new-list").appendTo('body');

UnderscoreJS - how to flatten a object using one of the iterator?

I would like to make a breadcrumb. And I am getting a data from back end as like this:
var object = {
"Department": {
"DeptCode": null,
"Description": "123",
"DeptName": {
"name": "xyz"
}
}}
And using underscore or jquery I would like to modify the object like this:
var result = {
"Department.DeptCode": null,
"Department.Description": "123",
"Department.DeptName.name": "xyz"
}
I tried using undrscore, but i am not get any result. any one show me the possible way to get this done?
my try:
var lable = [];
_.each(object, function(key, obj){
var title = obj;
if(_.object(key)){
_.each(key, function(text,obj){
lable.push(title + '.' + obj + ':' + text);
})
}
});
console.log(lable);
Here is the fiddle
Try creating a recursive function, like this (based on your fiddle):
var lable = {};
function flatten(object, title) {
_.each(object, function(key, obj){
var newTitle = obj;
if (key != null && typeof(key)=='object') {
flatten(key, (title) ? title + '.' + newTitle : newTitle);
} else {
lable[title + '.' + newTitle] = key;
}
});
};
flatten(object, '');
console.log(lable);
Check this fiddle: http://jsfiddle.net/ACZs8/2/
Another alternative, replacing the global variable approach with closures, to define a more reusable flatting function:
function flatten(mainObject) {
var lable = {};
flattenAux = function(object, title) {
_.each(object, function(key, obj){
var newTitle = obj;
if (key != null && typeof(key)=='object') {
flattenAux(key, (title) ? title + '.' + newTitle : newTitle);
} else {
lable[title + '.' + newTitle] = key;
}
});
};
flattenAux(mainObject, null);
return lable;
}
var testResult = flatten(object);
console.log(testResult);
Example fiddle: http://jsfiddle.net/ACZs8/6/

converting javascript array into html table in new window on click

I am creating data visualizations, and I have a button that will execute a JavaScript function and extract the first five rows from specified columns of data based on the user's selection:
getselection.onclick = function()
{
visual.Document.selection.getselection(
"City", \\identifies the selection
"Users", \\identifies the table
["Name", "Phone", "Zipcode"], \\identifies the columns
5,
function(args)
{
log(dump(args, "visual.Document.selection.getselection", 2));
});
};
The resulting output looks like this:
[Name]
0: xxxxx
1: xxxxx
2: xxxxx
[Phone]
0: xxxxx
1: xxxxx
What I would like to do is to display the results of the users selection in an HTML table in a new window that opens upon click. I have seen a suggestions for doing something similar to this, but for some reason I can't seem to get them to work. Here is what I have so far:
function getSelMarking() {
visual.Document.selection.getMarking(
"city",
"Users",
["Name", "phone", "Zipcode"],
5,
function(args) {
var UserIDs=dump(args);
HTMLstring='<HTML>\n';
HTMLstring+='<HEAD>\n';
HTMLstring+='<TITLE>New Document</TITLE>\n';
HTMLstring+='</HEAD>\n';
HTMLstring+='<BODY>\n';
HTMLstring+='<P>Hello World</P>\n';
HTMLstring+='<table>\n';
HTMLstring+='<tr>\n';
HTMLstring+='<td>'+UserIDs+'</td>\n';
HTMLstring+='<td>'+UserIDs+'</td>\n';
HTMLstring+='<td>'+UserIDs+'</td>\n';
HTMLstring+='</tr>\n';
HTMLstring+='</table>\n';
HTMLstring+='</BODY>\n';
HTMLstring+='</HTML>';
newwindow=window.open();
newdocument=newwindow.document;
newdocument.write(HTMLstring);
newdocument.close();
}
);
}
Thats as far as I've gotten. I am completely stuck on this - maybe I just don't have a good understanding of how the functions work? Regardless, thank you to anyone who can lend any type of assistance here.
i forgot to include the breakdown of the dump() function:
var dump = function(obj, name, indent, depth) {
if (depth > MAX_DUMP_DEPTH) {
return indent + name + ": <Maximum Depth Reached>\n";
}
if (typeof(obj) == "object") {
var child = null;
var output = name + "\n";
indent += "\t";
for (var item in obj) {
try {
if (item.charAt(0) == '_') {
continue;
}
child = obj[item];
} catch (e) {
child = "<Unable to Evaluate>";
}
if (typeof child == "object") {
output += dump(child, item, indent, depth + 1);
} else if (typeof(child) == "function") {
var functionName = String(child);
functionName = functionName.substring(0, functionName.indexOf("{", 0) -
10}
output += "\t" + item + ": " + functionName + "\n";
} else {
var value = "";
if (child == null) {
value = "[null]";
} else {
value = child;
}
output += "\t" + item + ": " + value + "\n";
}
}
return output + "\n";
} else {
return obj;
}
};
I found the answer to my own question. the difficulty here was caused my lack of understanding as to how javascript handled arrays. sorry everyone.

Is there any native function to convert json to url parameters?

I need convert json object to url form like: "parameter=12&asd=1"
I done with this:
var data = {
'action':'actualiza_resultado',
'postID': 1,
'gl': 2,
'gl2' : 3
};
var string_=JSON.stringify(data);
string_=string_.replace(/{/g, "");
string_=string_.replace(/}/g, "");
string_=string_.replace(/:/g, "=")
string_=string_.replace(/,/g, "&");
string_=string_.replace(/"/g, "");
But i wonder if there any function in javascript or in JSON object to do this?
Use the URLSearchParams interface, which is built into browsers and Node.js starting with version 10, released in 2018.
const myParams = {'foo': 'hi there', 'bar': '???'};
const u = new URLSearchParams(myParams).toString();
console.log(u);
Old answer: jQuery provides param that does exactly that. If you don't use jquery, take at look at the source.
Basically, it goes like this:
url = Object.keys(data).map(function(k) {
return encodeURIComponent(k) + '=' + encodeURIComponent(data[k])
}).join('&')
Using ES6 syntax:
var data = {
'action':'actualiza_resultado',
'postID': 1,
'gl': 2,
'gl2' : 3
};
let urlParameters = Object.entries(data).map(e => e.join('=')).join('&');
console.log(urlParameters);
I made an implementation that support nested objects and arrays i.e.
var data = {
users: [
{
"name": "jeff",
"tasks": [
"Do one thing",
"Do second thing"
]
},
{
"name": "rick",
"tasks": [
"Never gonna give you up",
"Never gonna let you down"
]
}
]
}
Will be:
users[0][name]=jeff&users[0][tasks][0]=Do%20one%20thing&users[0][tasks][1]=Do%20second%20thing&users[1][name]=rick&users[1][tasks][0]=Never%20gonna%20give%20you%20up&users[1][tasks][1]=Never%20gonna%20let%20you%20down
So, here's the implementation:
var isObj = function(a) {
if ((!!a) && (a.constructor === Object)) {
return true;
}
return false;
};
var _st = function(z, g) {
return "" + (g != "" ? "[" : "") + z + (g != "" ? "]" : "");
};
var fromObject = function(params, skipobjects, prefix) {
if (skipobjects === void 0) {
skipobjects = false;
}
if (prefix === void 0) {
prefix = "";
}
var result = "";
if (typeof(params) != "object") {
return prefix + "=" + encodeURIComponent(params) + "&";
}
for (var param in params) {
var c = "" + prefix + _st(param, prefix);
if (isObj(params[param]) && !skipobjects) {
result += fromObject(params[param], false, "" + c);
} else if (Array.isArray(params[param]) && !skipobjects) {
params[param].forEach(function(item, ind) {
result += fromObject(item, false, c + "[" + ind + "]");
});
} else {
result += c + "=" + encodeURIComponent(params[param]) + "&";
}
}
return result;
};
var data = {
users: [{
"name": "jeff",
"tasks": [
"Do one thing",
"Do second thing"
]
},
{
"name": "rick",
"tasks": [
"Never gonna give you up",
"Never gonna let you down"
]
}
]
}
document.write(fromObject(data));
You don't need to serialize this object literal.
Better approach is something like:
function getAsUriParameters(data) {
var url = '';
for (var prop in data) {
url += encodeURIComponent(prop) + '=' +
encodeURIComponent(data[prop]) + '&';
}
return url.substring(0, url.length - 1)
}
getAsUriParameters(data); //"action=actualiza_resultado&postID=1&gl=2&gl2=3"
Something I find nicely looking in ES6:
function urlfy(obj) {
return Object
.keys(obj)
.map(k => `${encodeURIComponent(k)}=${encodeURIComponent(obj[k])}`)
.join('&');
}
Later update (same thing, maybe a bit cleaner):
const urlfy = obj => Object
.keys(obj)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(obj[k]))
.join('&');
Like #georg said, you can use JQuery.param for flat objects.
If you need to process complex objects, you can use JsonUri, a python package that does just that. There is JavaScript library for it as well
Disclaimer: I am the author of JSONURI
Edit: I learned much later that you can also just base64 encode your payload - most languages as support for base64 encoding/decoding
Example
x = {name: 'Petter', age: 47, places: ['Mozambique', 'Zimbabwe']}
stringRep = JSON.stringify(x)
encoded = window.btoa(stringRep)
Gives you eyJuYW1lIjoiUGV0dGVyIiwiYWdlIjo0NywicGxhY2VzIjpbIk1vemFtYmlxdWUiLCJaaW1iYWJ3ZSJdfQ==, which you can use as a uri parameter
decoded = window.atob(encoded)
originalX = JSON.parse(decoded)
Needless to say, it comes with its own caveats
But i wonder if there any function in javascript
Nothing prewritten in the core.
or json to do this?
JSON is a data format. It doesn't have functions at all.
This is a relatively trivial problem to solve though, at least for flat data structures.
Don't encode the objects as JSON, then:
function obj_to_query(obj) {
var parts = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]));
}
}
return "?" + parts.join('&');
}
alert(obj_to_query({
'action': 'actualiza_resultado',
'postID': 1,
'gl': 2,
'gl2': 3
}));
There isn't a standard way to encode complex data structures (e.g. with nested objects or arrays). It wouldn't be difficult to extend this to emulate the PHP method (of having square brackets in field names) or similar though.
This one processes arrays with by changing the nameinto mutiple name[]
function getAsUriParameters (data) {
return Object.keys(data).map(function (k) {
if (_.isArray(data[k])) {
var keyE = encodeURIComponent(k + '[]');
return data[k].map(function (subData) {
return keyE + '=' + encodeURIComponent(subData);
}).join('&');
} else {
return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]);
}
}).join('&');
};
Best solution for Vanilla JavaScript:
var params = Object.keys(data)
.filter(function (key) {
return data[key] ? true : false
})
.map(function (key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(data[key])
})
.join('&');
PS: The filter is used here to remove null or undefined parameters. It makes the url look cleaner.
The custom code above only handles flat data. And JQuery is not available in react native. So here is a js solution that does work with multi-level objects and arrays in react native.
function formurlencoded(data) {
const opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
let sorted = Boolean(opts.sorted),
skipIndex = Boolean(opts.skipIndex),
ignorenull = Boolean(opts.ignorenull),
encode = function encode(value) {
return String(value).replace(/(?:[\0-\x1F"-&\+-\}\x7F-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])/g, encodeURIComponent).replace(/ /g, '+').replace(/[!'()~\*]/g, function (ch) {
return '%' + ch.charCodeAt().toString(16).slice(-2).toUpperCase();
});
},
keys = function keys(obj) {
const keyarr = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Object.keys(obj);
return sorted ? keyarr.sort() : keyarr;
},
filterjoin = function filterjoin(arr) {
return arr.filter(function (e) {
return e;
}).join('&');
},
objnest = function objnest(name, obj) {
return filterjoin(keys(obj).map(function (key) {
return nest(name + '[' + key + ']', obj[key]);
}));
},
arrnest = function arrnest(name, arr) {
return arr.length ? filterjoin(arr.map(function (elem, index) {
return skipIndex ? nest(name + '[]', elem) : nest(name + '[' + index + ']', elem);
})) : encode(name + '[]');
},
nest = function nest(name, value) {
const type = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : typeof value === 'undefined' ? 'undefined' : typeof(value);
let f = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
if (value === f) f = ignorenull ? f : encode(name) + '=' + f; else if (/string|number|boolean/.test(type)) f = encode(name) + '=' + encode(value); else if (Array.isArray(value)) f = arrnest(name, value); else if (type === 'object') f = objnest(name, value);
return f;
};
return data && filterjoin(keys(data).map(function (key) {
return nest(key, data[key]);
}));
}
The conversion from a JSON string to a URL query string can be done in a single line:
const json = '{"action":"actualiza_resultado","postID":1,"gl":2,"gl2":3}';
const queryString = new URLSearchParams(JSON.parse(json)).toString();
queryString would then be set to "action=actualiza_resultado&postID=1&gl=2&gl2=3".
Based on georg's answer, but also adding ? before the string and using ES6:
const query = !params ? '': Object.keys(params).map((k, idx) => {
let prefix = '';
if (idx === 0) {
prefix = '?';
}
return prefix + encodeURIComponent(k) + '=' + encodeURIComponent(params[k]);
}).join('&');
As most of the answers only convert flat objects to query parameters, I would like to share mine.
This function can handle flat objects, as well as nested arrays/objects while only using plain JS.
function incapsulateInBrackets(key)
{
return '[' + key + ']';
}
function encode(object, isSubEncode=false, prefix = '')
{
let parts = Object.keys(object).map( (key) => {
let encodedParts = [];
if(Array.isArray(object[key]))
{
object[key].map(function(innerKey, index){
encodedParts.push( encode(object[key][index], true, prefix + key + incapsulateInBrackets(index)));
});
}
else if(object[key] instanceof Object)
{
Object.keys(object[key]).map( (innerKey) => {
if(Array.isArray(object[key][innerKey]))
{
encodedParts.push( encode(object[key][index], true, prefix + incapsulateInBrackets(key) + incapsulateInBrackets(innerKey)) );
}
else
{
encodedParts.push( prefix + incapsulateInBrackets(key) + incapsulateInBrackets(innerKey) + '=' + object[key][innerKey] );
}
});
}
else
{
if(isSubEncode)
{
encodedParts.push( prefix + incapsulateInBrackets(key) + '=' + object[key] );
}
else
{
encodedParts.push( key + '=' + object[key] );
}
}
return encodedParts.join('&');
});
return parts.join('&');
}
Make a utility if you have nodejs
const querystring = require('querystring')
export function makeQueryString(params): string {
return querystring.stringify(params)
}
import example
import { makeQueryString } from '~/utils'
example of use
makeQueryString({
...query,
page
})
Read the latest documentation here.

Categories

Resources