Solved

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

Posted on 2015-02-22
10
646 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
Comment Utility
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
Comment Utility
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
Comment Utility
Delphi XE7 Compiler says:

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

Open in new window

0
 

Author Comment

by:PeterDelphin
Comment Utility
DElphi XE7 needs System.JSON in uses clause.
0
 

Author Comment

by:PeterDelphin
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 24

Expert Comment

by:jimyX
Comment Utility
uses data.dbxjson;
0
 
LVL 24

Expert Comment

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

Author Comment

by:PeterDelphin
Comment Utility
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
Comment Utility
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
Comment Utility
Great!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

16 Experts available now in Live!

Get 1:1 Help Now