Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2015-02-22
10
Medium Priority
?
1,319 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 2000 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
Independent Software Vendors: 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

609 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