Created
April 21, 2015 06:27
-
-
Save felthy/4b36fbd318da8a094426 to your computer and use it in GitHub Desktop.
Recursively dump a javascript object into a string, for when you don't have a debugger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* When investigating a selenium test failure on a remote headless browser that couldn't be reproduced | |
* locally, I wanted to add some javascript to the site under test that would dump some state to the | |
* page (so it could be captured by Selenium as a screenshot when the test failed). JSON.stringify() | |
* didn't work because the object declared a toJSON() method, and JSON.stringify() just calls that | |
* method if it's present. This was a Moment object, so toJSON() returned a string but I wanted to see | |
* the internal state of the object instead. | |
* | |
* So, this is a rough and ready function that recursively dumps any old javascript object. | |
*/ | |
function printObject(o, indent) { | |
var out = ''; | |
if (typeof indent === 'undefined') { | |
indent = 0; | |
} | |
for (var p in o) { | |
if (o.hasOwnProperty(p)) { | |
var val = o[p]; | |
out += new Array(4 * indent + 1).join(' ') + p + ': '; | |
if (typeof val === 'object') { | |
if (val instanceof Date) { | |
out += 'Date "' + val.toISOString() + '"'; | |
} else { | |
out += '{\n' + printObject(val, indent + 1) + new Array(4 * indent + 1).join(' ') + '}'; | |
} | |
} else if (typeof val === 'function') { | |
} else { | |
out += '"' + val + '"'; | |
} | |
out += ',\n'; | |
} | |
} | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
when I tried to used the above printObject function to print this sample JSON string/object, it does not seem to work.
Here is the input JSON object
"{ "feds": { "_id": { "$oid": "5b228ba1e34769022c67aa6b" }, "fed_deps": [ { "fed_dep_file": "deployment_fileserver.yaml", "fed_dep_name": "fileserver", "images": [ { "image": "10.32.7.60:5000/cna_builder/workspace/main_int/cna-fileserver-dbg:latest", "image_id": "1" } ], "replicas": "1" }, { "fed_dep_file": "deployment_tester.yaml", "fed_dep_name": "tester", "images": [ { "image": "10.32.7.60:5000/cna_builder/workspace/main_int/cna-fileserver-dbg:latest", "image_id": "1" } ], "replicas": "1" }, { "fed_dep_file": "deployment_srvreg.yaml", "fed_dep_name": "srvreg-dep", "images": [ { "image": "10.32.7.60:5000/cna_builder/workspace/main_int/cna-service_reg-service_reg-dbg:latest", "image_id": "1" } ], "replicas": "1" }, { "fed_dep_file": "deployment_hello.yaml", "fed_dep_name": "hello-dep", "images": [ { "image": "10.32.7.60:5000/cna_builder/workspace/main_int/cna-infra-dbg:latest", "image_id": "1" }, { "image": "10.32.7.60:5000/cna_builder/workspace/main_int/cna-hello_world-dbg:latest", "image_id": "2" } ], "replicas": "1" }, { "fed_dep_file": "deployment_goodbye.yaml", "fed_dep_name": "goodbye-dep", "images": [ { "image": "10.32.7.60:5000/cna_builder/workspace/main_int/cna-infra-dbg:latest", "image_id": "1" }, { "image": "10.32.7.60:5000/cna_builder/workspace/main_int/cna-goodbye_world-dbg:latest", "image_id": "2" } ], "replicas": "1" }, { "fed_dep_file": "deployment_smfcc.yaml", "fed_dep_name": "smfcc-dep", "images": [ { "image": "10.32.7.60:5000/cna_builder/workspace/main_int/cna-infra-dbg:latest", "image_id": "1" }, { "image": "10.32.7.60:5000/cna_builder/workspace/main_int/cna-smf_call_control-dbg:latest", "image_id": "2" } ], "replicas": "1" }, { "fed_dep_file": "deployment_cfgmgr.yaml", "fed_dep_name": "cfgmgr-dep", "images": [ { "image": "10.32.7.60:5000/cna_builder/workspace/main_int/cna-infra-dbg:latest", "image_id": "1" }, { "image": "10.32.7.60:5000/cna_builder/workspace/main_int/cna-cfgmgr-cfgmgr-dbg:latest", "image_id": "2" } ], "replicas": "1" } ], "fed_dir": "/vnfmgr/feds/generic_fed/1", "fed_id": "generic_fed", "fed_name": "Generic Federation", "fed_namespace": "generic", "fed_pv": [ { "class": "web", "clusterIp": "0", "externalIP": "10.9.22.100", "fed_pv_file": "nfswebpv.yaml", "fed_pv_name": "fileserver-web-pv", "size": "5Gi" }, { "class": "cfg", "clusterIp": "0", "externalIP": "10.9.22.100", "fed_pv_file": "nfscfgpv.yaml", "fed_pv_name": "fileserver-cfg-pv", "size": "5Gi" } ], "fed_pvc": [ { "fed_pvc_file": "nfspwebpvc.yaml", "fed_pvc_name": "fileserver-web-pvc", "size": "5Gi" }, { "fed_pvc_file": "nfspcfgpvc.yaml", "fed_pvc_name": "fileserver-cfg-pvc", "size": "5Gi" } ], "fed_srvs": [ { "clusterIp": "0", "externalIP": "10.9.22.100", "externalPort": "85", "fed_srv_file": "service_fileserver.yaml", "fed_srv_name": "fileserver-svc" } ] } }"
and this is the output I got:
0: """,
1: "{",
2: " ",
3: "",
4: """,
5: "f",
6: "e",
7: "d",
8: "s",
9: "",
10: """,
11: ":",
12: " ",
13: "{",
14: " ",
15: "",
16: """,
17: "",
18: "i",
19: "d",
20: "",
21: """,
22: ":",
23: " ",
24: "{",
25: " ",
26: "",
27: """,
28: "$",
29: "o",
30: "i",
31: "d",
32: "",
33: """,
34: ":",
35: " ",
36: "",
37: """,
38: "5",
39: "b",
40: "2",
41: "2",
42: "8",
43: "b",
44: "a",
45: "1",
46: "e",
47: "3",
48: "4",
49: "7",
50: "6",
51: "9",
52: "0",
53: "2",
54: "2",
55: "c",
56: "6",
57: "7",
58: "a",
59: "a",
60: "6",
61: "b",
62: "",
63: """,
64: " ",
65: "}",
66: ",",
67: " ",
68: "",
69: """,
70: "f",
71: "e",
72: "d",
73: "",
74: "d",
75: "e",
76: "p",
77: "s",
78: "",
79: """,
80: ":",
81: " ",
82: "[",
83: " ",
84: "{",
85: " ",
86: "",
87: """,
88: "f",
89: "e",
90: "d",
91: "",
92: "d",
93: "e",
94: "p",
95: "",
96: "f",
97: "i",
98: "l",
99: "e",
100: "",
101: """,
102: ":",
103: " ",
104: "",
105: """,
106: "d",
107: "e",
108: "p",
109: "l",
110: "o",
111: "y",
112: "m",
113: "e",
114: "n",
115: "t",
116: "",
117: "f",
118: "i",
119: "l",
120: "e",
121: "s",
122: "e",
123: "r",
124: "v",
125: "e",
126: "r",
127: ".",
128: "y",
129: "a",
130: "m",
131: "l",
132: "",
133: """,
134: ",",
135: " ",
136: "",
137: """,
138: "f",
139: "e",
140: "d",
141: "",
142: "d",
143: "e",
144: "p",
145: "",
146: "n",
147: "a",
148: "m",
149: "e",
150: "",
151: """,
152: ":",
153: " ",
154: "",
155: """,
156: "f",
157: "i",
158: "l",
159: "e",
160: "s",
161: "e",
162: "r",
163: "v",
164: "e",
165: "r",
166: "",
167: """,
168: ",",
169: " ",
170: "",
171: """,
172: "i",
173: "m",
174: "a",
175: "g",
176: "e",
177: "s",
178: "",
179: """,
180: ":",
181: " ",
182: "[",
183: " ",
184: "{",
185: " ",
186: "",
187: """,
188: "i",
189: "m",
190: "a",
191: "g",
192: "e",
193: "",
194: """,
195: ":",
196: " ",
197: "",
198: """,
199: "1",
200: "0",
201: ".",
202: "3",
203: "2",
204: ".",
205: "7",
206: ".",
207: "6",
208: "0",
209: ":",
210: "5",
211: "0",
212: "0",
213: "0",
214: "/",
215: "c",
216: "n",
217: "a",
218: "",
219: "b",
220: "u",
221: "i",
222: "l",
223: "d",
224: "e",
225: "r",
226: "/",
227: "w",
228: "o",
229: "r",
230: "k",
231: "s",
232: "p",
233: "a",
234: "c",
235: "e",
236: "/",
237: "m",
238: "a",
239: "i",
240: "n",
241: "",
242: "i",
243: "n",
244: "t",
245: "/",
246: "c",
247: "n",
248: "a",
249: "-",
250: "f",
251: "i",
252: "l",
253: "e",
254: "s",
255: "e",
256: "r",
257: "v",
258: "e",
259: "r",
260: "-",
261: "d",
262: "b",
263: "g",
264: ":",
265: "l",
266: "a",
267: "t",
268: "e",
269: "s",
270: "t",
271: "",
272: """,
273: ",",
274: " ",
275: "",
276: """,
277: "i",
278: "m",
279: "a",
280: "g",
281: "e",
282: "",
283: "i",
284: "d",
285: "",
286: """,
287: ":",
288: " ",
289: "",
290: """,
291: "1",
292: "",
293: """,
294: " ",
295: "}",
296: " ",
297: "]",
298: ",",
299: " ",
300: "",
301: """,
302: "r",
303: "e",
304: "p",
305: "l",
306: "i",
307: "c",
308: "a",
309: "s",
310: "",
311: """,
312: ":",
313: " ",
314: "",
315: """,
316: "1",
317: "",
318: """,
319: " ",
320: "}",
321: ",",
322: " ",
323: "{",
324: " ",
325: "",
326: """,
327: "f",
328: "e",
329: "d",
330: "",
331: "d",
332: "e",
333: "p",
334: "",
335: "f",
336: "i",
337: "l",
338: "e",
339: "",
340: """,
341: ":",
342: " ",
343: "",
344: """,
345: "d",
346: "e",
347: "p",
348: "l",
349: "o",
350: "y",
351: "m",
352: "e",
353: "n",
354: "t",
355: "",
356: "t",
357: "e",
358: "s",
359: "t",
360: "e",
361: "r",
362: ".",
363: "y",
364: "a",
365: "m",
366: "l",
367: "",
368: """,
369: ",",
370: " ",
371: "",
372: """,
373: "f",
374: "e",
375: "d",
376: "",
377: "d",
378: "e",
379: "p",
380: "",
381: "n",
382: "a",
383: "m",
384: "e",
385: "",
386: """,
387: ":",
388: " ",
389: "",
390: """,
391: "t",
392: "e",
393: "s",
394: "t",
395: "e",
396: "r",
397: "",
398: """,
399: ",",
400: " ",
401: "",
402: """,
403: "i",
404: "m",
405: "a",
406: "g",
407: "e",
408: "s",
409: "",
410: """,
411: ":",
412: " ",
413: "[",
414: " ",
415: "{",
416: " ",
417: "",
418: """,
419: "i",
420: "m",
421: "a",
422: "g",
423: "e",
424: "",
425: """,
426: ":",
427: " ",
428: "",
429: """,
430: "1",
431: "0",
432: ".",
433: "3",
434: "2",
435: ".",
436: "7",
437: ".",
438: "6",
439: "0",
440: ":",
441: "5",
442: "0",
443: "0",
444: "0",
445: "/",
446: "c",
447: "n",
448: "a",
449: "",
450: "b",
451: "u",
452: "i",
453: "l",
454: "d",
455: "e",
456: "r",
457: "/",
458: "w",
459: "o",
460: "r",
461: "k",
462: "s",
463: "p",
464: "a",
465: "c",
466: "e",
467: "/",
468: "m",
469: "a",
470: "i",
471: "n",
472: "",
473: "i",
474: "n",
475: "t",
476: "/",
477: "c",
478: "n",
479: "a",
480: "-",
481: "f",
482: "i",
483: "l",
484: "e",
485: "s",
486: "e",
487: "r",
488: "v",
489: "e",
490: "r",
491: "-",
492: "d",
493: "b",
494: "g",
495: ":",
496: "l",
497: "a",
498: "t",
499: "e",
500: "s",
501: "t",
502: "",
503: """,
504: ",",
505: " ",
506: "",
507: """,
508: "i",
509: "m",
510: "a",
511: "g",
512: "e",
513: "",
514: "i",
515: "d",
516: "",
517: """,
518: ":",
519: " ",
520: "",
521: """,
522: "1",
523: "",
524: """,
525: " ",
526: "}",
527: " ",
528: "]",
529: ",",
530: " ",
531: "",
532: """,
533: "r",
534: "e",
535: "p",
536: "l",
537: "i",
538: "c",
539: "a",
540: "s",
541: "",
542: """,
543: ":",
544: " ",
545: "",
546: """,
547: "1",
548: "",
549: """,
550: " ",
551: "}",
552: ",",
553: " ",
554: "{",
555: " ",
556: "",
557: """,
558: "f",
559: "e",
560: "d",
561: "",
562: "d",
563: "e",
564: "p",
565: "",
566: "f",
567: "i",
568: "l",
569: "e",
570: "",
571: """,
572: ":",
573: " ",
574: "",
575: """,
576: "d",
577: "e",
578: "p",
579: "l",
580: "o",
581: "y",
582: "m",
583: "e",
584: "n",
585: "t",
586: "",
587: "s",
588: "r",
589: "v",
590: "r",
591: "e",
592: "g",
593: ".",
594: "y",
595: "a",
596: "m",
597: "l",
598: "",
599: """,
600: ",",
601: " ",
602: "",
603: """,
604: "f",
605: "e",
606: "d",
607: "",
608: "d",
609: "e",
610: "p",
611: "",
612: "n",
613: "a",
614: "m",
615: "e",
616: "",
617: """,
618: ":",
619: " ",
620: "",
621: """,
622: "s",
623: "r",
624: "v",
625: "r",
626: "e",
627: "g",
628: "-",
629: "d",
630: "e",
631: "p",
632: "",
633: """,
634: ",",
635: " ",
636: "",
637: """,
638: "i",
639: "m",
640: "a",
641: "g",
642: "e",
643: "s",
644: "",
645: """,
646: ":",
647: " ",
648: "[",
649: " ",
650: "{",
651: " ",
652: "",
653: """,
654: "i",
655: "m",
656: "a",
657: "g",
658: "e",
659: "",
660: """,
661: ":",
662: " ",
663: "",
664: """,
665: "1",
666: "0",
667: ".",
668: "3",
669: "2",
670: ".",
671: "7",
672: ".",
673: "6",
674: "0",
675: ":",
676: "5",
677: "0",
678: "0",
679: "0",
680: "/",
681: "c",
682: "n",
683: "a",
684: "",
685: "b",
686: "u",
687: "i",
688: "l",
689: "d",
690: "e",
691: "r",
692: "/",
693: "w",
694: "o",
695: "r",
696: "k",
697: "s",
698: "p",
699: "a",
700: "c",
701: "e",
702: "/",
703: "m",
704: "a",
705: "i",
706: "n",
707: "",
708: "i",
709: "n",
710: "t",
711: "/",
712: "c",
713: "n",
714: "a",
715: "-",
716: "s",
717: "e",
718: "r",
719: "v",
720: "i",
721: "c",
722: "e",
723: "",
724: "r",
725: "e",
726: "g",
727: "-",
728: "s",
729: "e",
730: "r",
731: "v",
732: "i",
733: "c",
734: "e",
735: "",
736: "r",
737: "e",
738: "g",
739: "-",
740: "d",
741: "b",
742: "g",
743: ":",
744: "l",
745: "a",
746: "t",
747: "e",
748: "s",
749: "t",
750: "",
751: """,
752: ",",
753: " ",
754: "",
755: """,
756: "i",
757: "m",
758: "a",
759: "g",
760: "e",
761: "",
762: "i",
763: "d",
764: "",
765: """,
766: ":",
767: " ",
768: "",
769: """,
770: "1",
771: "",
772: """,
773: " ",
774: "}",
775: " ",
776: "]",
777: ",",
778: " ",
779: "",
780: """,
781: "r",
782: "e",
783: "p",
784: "l",
785: "i",
786: "c",
787: "a",
788: "s",
789: "",
790: """,
791: ":",
792: " ",
793: "",
794: """,
795: "1",
796: "",
797: """,
798: " ",
799: "}",
800: ",",
801: " ",
802: "{",
803: " ",
804: "",
805: """,
806: "f",
807: "e",
808: "d",
809: "",
810: "d",
811: "e",
812: "p",
813: "",
814: "f",
815: "i",
816: "l",
817: "e",
818: "",
819: """,
820: ":",
821: " ",
822: "",
823: """,
824: "d",
825: "e",
826: "p",
827: "l",
828: "o",
829: "y",
830: "m",
831: "e",
832: "n",
833: "t",
834: "",
835: "h",
836: "e",
837: "l",
838: "l",
839: "o",
840: ".",
841: "y",
842: "a",
843: "m",
844: "l",
845: "",
846: """,
847: ",",
848: " ",
849: "",
850: """,
851: "f",
852: "e",
853: "d",
854: "",
855: "d",
856: "e",
857: "p",
858: "_",
859: "n",
860: "a",
861: "m",
862: "e",
863: "",
864: """,
865: ":",
866: " ",
867: "",
868: """,
869: "h",
870: "e",
871: "l",
872: "l",
873: "o",
874: "-",
875: "d",
876: "e",
877: "p",
878: "",
879: """,
880: ",",
881: " ",
882: "",
883: """,
884: "i",
885: "m",
886: "a",
887: "g",
888: "e",
889: "s",
890: "",
891: """,
892: ":",
893: " ",
894: "[",
895: " ",
896: "{",
897: " ",
898: "",
899: """,
900: "i",
901: "m",
902: "a",
903: "g",
...
",
how can i print them in key,value pairs?