Solved

How to extract only a specific value from a local JSON file?

Posted on 2015-02-22
10
890 Views
Last Modified: 2015-02-23
I have a file info.json which contains only this code:

{"personal": {"path": "C:\\My Directory", "honk": 82641923}}

Open in new window


In Delphi XE7, using a JSON library, which is the most simple way to extract the path value, i.e. C:\My Directory?
0
Comment
Question by:PeterDelphin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 24

Accepted Solution

by:
jimyX earned 500 total points
ID: 40625071
If that is a constant then:
uses DBXJSON;

function ParseJsonStr(jStr, jPar1, jPar2: String): String;
var
  jObj: TJSONObject;
  jVal: TJSONValue;
begin
  Result:= '';
  jObj := TJSONObject.ParseJSONValue(jStr) as TJSONObject;
  try
    jVal:= jObj.Get(jPar1).JsonValue;
    Result:= TJSONObject(jVal).Get(jPar2).JsonValue.Value;
  finally
    jObj.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Text:= ParseJsonStr('{"personal": {"path": "C:\\My Directory", "honk": 82641923}}',
                            'personal',
                            'path');
end;

Open in new window


Otherwise loop through the Objects and check value arrays.
0
 
LVL 24

Expert Comment

by:jimyX
ID: 40625074
Simpler than that could be:
function ParseJsonStr(jStr: String): String;
var
  jObj: TJSONObject;
  jVal: TJSONValue;
begin
  Result:= '';
  jObj := TJSONObject.ParseJSONValue(jStr) as TJSONObject;
  try
    jVal:= jObj.Get(0).JsonValue;
    Result:= TJSONObject(jVal).Get(0).JsonValue.Value;
  finally
    jObj.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Text:= ParseJsonStr('{"personal": {"path": "C:\\My Directory", "honk": 82641923}}'); // or pass the positions as params ", 0, 0"
end;

Open in new window

0
 

Author Comment

by:PeterDelphin
ID: 40625131
Delphi XE7 Compiler says:

[dcc32 Error] MainForm.pas(453): E2003 Undeclared identifier: 'TJSONObject'

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:PeterDelphin
ID: 40625132
DElphi XE7 needs System.JSON in uses clause.
0
 

Author Comment

by:PeterDelphin
ID: 40625157
In the second example you are iterating true the parts of the JSON object by using the ordinal value (0).

But the parts could also be in a different order. Is it possible to parse the JSON object by looking specifically for a pair where the name is "path" and then just extract the value of this pair?
0
 
LVL 24

Expert Comment

by:jimyX
ID: 40625160
uses data.dbxjson;
0
 
LVL 24

Expert Comment

by:jimyX
ID: 40625164
To use the second example the order must always be the same, otherwise use the first example.
0
 

Author Comment

by:PeterDelphin
ID: 40625171
When using data.dbxjson instead of System.JSON then the DelphiXE7 compiler says:

[dcc32 Error] MainForm.pas(454): E2003 Undeclared identifier: 'TJSONObject'
0
 
LVL 24

Expert Comment

by:jimyX
ID: 40625179
I do not have XE7. If System.JSON works then System.JSON is the unit to use.

Seems data.dbxjson has been changed from previous versions.
0
 

Author Closing Comment

by:PeterDelphin
ID: 40625202
Great!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question