MATLAB Answers

Convert Struct to Readable JSON(Pretty Print)

225 views (last 30 days)
ILoveMATLAB
ILoveMATLAB on 4 Sep 2019
Commented: ILoveMATLAB on 18 Mar 2021
Every struct I pass to jsonecode gets converted to a 1 by N char array with no newline chacters. Therefore, when I export the json as to a text file I only have one line. The file is not easly readble because I have to scroll the JSON text horizontaly. How can I make jsonencode's output easily readable(pretty print)?
I get this.
{ "firstName": "John", "lastName": "Smith", "age": 25, "address": { "streetAddress": "3 Apple Hill Dr", "city": "Natick", "state": "MA", "postalCode": "01760" }, "phoneNumber": [ { "type": "home", "number": "123 456 7890" }, { "type": "cell", "number": "098 765 4321" } ] }
I want something like this.
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address":
{
"streetAddress": "3 Apple Hill Dr",
"city": "Natick",
"state": "MA",
"postalCode": "01760"
},
"phoneNumber":
[
{
"type": "home",
"number": "123 456 7890"
},
{
"type": "cell",
"number": "098 765 4321"
}
]
}

Answers (2)

Gaurav Garg
Gaurav Garg on 20 Nov 2019
Hi,
You can find a function which can help you resolve the problem you are facing in this file.

Uday Shankar
Uday Shankar on 17 Mar 2021
clc;
clear;
close all;
tic
% 11-Bus distribution System
% %br se re r x
% LD=[1 1 2 0.04997 0.06644
% 2 2 3 0.02332 0.03310
% 3 1 4 0.04664 0.06201
% 4 1 5 0.02082 0.02768
% 5 5 6 0.02500 0.03322
% 6 1 7 0.02665 0.03543
% 7 7 8 0.02748 0.03654
% 8 1 9 0.03331 0.04430
% 9 1 10 0.02082 0.02768
% 10 2 11 0.02082 0.02768];
%
% % bus kw kVAR
% BD=[ 1 0 0
% 2 1.22 0.916
% 3 0.032 0.024
% 4 0.778 0.584
% 5 0.673 0.595
% 6 1.22 0.916
% 7 0.0488 0.0366
% 8 0.956 0.717
% 9 0.698 0.523
% 10 1.265 0.949
% 11 0.265 0.0949];
V_base = 12.66e3;
nbus = length(BD(:,1));
S = zeros(nbus,1);
P_BD = zeros(nbus,1);
Q_BD = zeros(nbus,1);
for k = 1:nbus
P_BD(k) = (BD(k,2)*1e3);
Q_BD(k) = (BD(k,3)*1e3);
S(k) = complex(P_BD(k),Q_BD(k));
end
%S=complex(BD(:,2),BD(:,3));% complex power
VB = 12.66e3 * ones(size(BD,1),1);% initial bus votage% 10 change to specific data value
Z=complex(LD(:,4),LD(:,5));%branch impedance
IB=zeros(size(LD,1),1);
It=100;
for i=1:It
%backward sweep
I=conj(S./VB);
for i=size(LD,1):-1:1
c=[];
e=[];
[c e]=find(LD(:,2:3)==LD(i,3));
if size(c,1)-1==0
IB(LD(i,1))=I(LD(i,3));
else
IB(LD(i,1))=I(LD(i,3))+sum(IB(LD(c,1)))-IB(LD(i,1));
end
end
% forward sweep
for i=1:size(LD,1)
VB(LD(i,3))=VB(LD(i,2))-IB(LD(i,1))*Z(i);
end
end
S_inj_ref = VB(1)*conj(IB(1));
Pkw = real(S_inj_ref);
QkvAR = imag(S_inj_ref);
temp1 = Pkw - sum(P_BD);
temp2 = QkvAR - sum(Q_BD);
I_bus = abs(IB);
temp3 = (I_bus).*real(Z);
temp4 = (I_bus).*imag(Z);
temp5 = (I_bus.^2).*real(Z);
temp6 = (I_bus.^2).*imag(Z);
Pflow=temp3*1e3;
Qflow=temp4*1e3;
Ptloss = temp1*1e3;
Qtloss = temp2*1e3;
V_bus = abs(VB)./V_base;
delta =angle(VB);
V_bus
delta
I_bus
Pflow
Qflow
Pkw
QkvAR
Ploss = temp5*1e3
Qloss = temp6*1e3
Pt = sum(Ploss)
Qt = sum (Qloss)
fprintf('\n\n Total real power load : %3.2f kW', sum(Pflow)/10^3);
fprintf('\n Total reactive power load : %3.2f kVAr', sum(Qflow)/10^3);
fprintf('\n\n Total real power loss : %3.3f kW', Pt/10^6);
fprintf('\n Total reactive power loss : %3.3f kVAr', Qt/10^6);
fprintf('\n\n Minimum Voltage : %3.5f\n\n',min(abs(V_bus)));
% For constant Power Load Modeling
k=input('Enter the type of load.0 for constant power/1 for consant current/2 for constant impedance :');
for i=It:It+1
Pflow_New=temp3*((V_bus.^k));
Qflow_New=temp4*((V_bus.^k));
end
Pflow_New
Qflow_New
toc
% % Cost Analysis
% % At each load_ Power Consumption per day
% S=Pflow+j*Qflow;
% S
% Price=input('Enter the unit cost in rupees= :');
% S_Cost_day = abs(S)*24*Price
% Total_Cost_Load_Day= sum(S_Cost_day)
% No_Days_Month =input('Enter No. of Days in the Month= :');
% Monthly_Load = S_Cost_day*No_Days_Month
% Total_Cost_Load_Month= sum(Monthly_Load)
% % Power Quality Analysis
% PQ=input('Enter the type of Power Quality Problem : ');
% if PQ = Volatage Sag
% for
% fault_bus=
%
% % fprintf('\n %d %4.4f %4.4f %6.2f %6.2f %6.2f %6.2f', nbus, abs(V_bus), angle(V_bus), Pflow, Qflow, Ploss, Qloss);
% % for n=1:nbus
% % fprintf('\n %d %8.4f %8.4f %8.2f %8.2f %8.2f %8.2f', nbus(n), abs(V_bus), angle(V_bus), Pflow(n), Qflow(n), Ploss(n), Qloss(n));
% % end

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by