Solved

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

Posted on 2015-02-22
10
698 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 103
Machine not responding during CopyFile() 3 88
When i run adoquery my application freezes 26 150
FMX and jaudiotracker playing memory stream 29 66
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

930 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now