Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic json examples
#1
Can QM interact/modify a JSON file?

Assuming I have the following json file:

File: c:\test.json

 
Code:
Copy      Help
{
    "version": 4,
    "lastUserContextId": 1,
    "identities": [{
            "userContextId": 1,
            "public": false,
            "icon": "txt",
            "color": "",
            "name": "test 1",
            "accessKey": ""
        }, {
            "userContextId": 2,
            "public": false,
            "icon": "txt",
            "color": "",
            "name": "test 2",
            "accessKey": ""
        }, {
            "userContextId": 3,
            "public": false,
            "icon": "txt",
            "color": "blue",
            "name": "test 3"
        }
    ]
}


Is it possible with QM to remove, add and modify and object?
I have the questions below in the code with the desired result
 
Code:
Copy      Help
1. Remove whole json object 'userContextId": 3'

RESULT:

{
    "version": 4,
    "lastUserContextId": 1,
    "identities": [{
            "userContextId": 1,
            "public": false,
            "icon": "txt",
            "color": "",
            "name": "test 1",
            "accessKey": ""
        }, {
            "userContextId": 2,
            "public": false,
            "icon": "txt",
            "color": "",
            "name": "test 2",
            "accessKey": ""
        }
    ]
}


2. Add object

            "userContextId": 7,
            "public": true,
            "icon": "doc",
            "color": "red",
            "name": "test 7",
            "accessKey": "123"
RESULT:

{
    "version": 4,
    "lastUserContextId": 1,
    "identities": [{
            "userContextId": 1,
            "public": false,
            "icon": "txt",
            "color": "",
            "name": "test 1",
            "accessKey": ""
        }, {
            "userContextId": 2,
            "public": false,
            "icon": "txt",
            "color": "",
            "name": "test 2",
            "accessKey": ""
        }, {
            "userContextId": 7,
            "public": true,
            "icon": "doc",
            "color": "red",
            "name": "test 7"
            "accessKey": "123"
        }
    ]
}



3. Modify key-value pair, of "userContextId": 1,"  ---> modify name from 'test 1' tot 'test 1B'

RESULT:
{
    "version": 4,
    "lastUserContextId": 1,
    "identities": [{
            "userContextId": 1,
            "public": false,
            "icon": "txt",
            "color": "",
            "name": "test B",  <===============
            .....
 
[/code]
#2
Need some JSON library. Many libraries exist for many languages, but difficult to find something that can be easy enough to use in QM. It is one of reasons why QM3 will use C# and .NET.
#3
Ok! thank you for the info!
I'll use string actions until then!
#4
Ron you can use Chilkat with qm to process json data. there is an activex com component or a .net version. The .net version is a bit easier but requires you to use CsScript.

here is all your request using the ChilkatDotNet47.dll .Just place it in qm program folder
here is the zip file containing the dll.

.zip   chilkatdotnet47-9.5.0-win32.zip (Size: 3.2 MB / Downloads: 191)
Function ChilkatJsonExamples
Code:
Copy      Help
out
str json=
;{
;;;;"version": 4,
;;;;"lastUserContextId": 1,
;;;;"identities": [{
;;;;;;;;;;;;"userContextId": 1,
;;;;;;;;;;;;"public": false,
;;;;;;;;;;;;"icon": "txt",
;;;;;;;;;;;;"color": "",
;;;;;;;;;;;;"name": "test 1",
;;;;;;;;;;;;"accessKey": ""
;;;;;;;;}, {
;;;;;;;;;;;;"userContextId": 2,
;;;;;;;;;;;;"public": false,
;;;;;;;;;;;;"icon": "txt",
;;;;;;;;;;;;"color": "",
;;;;;;;;;;;;"name": "test 2",
;;;;;;;;;;;;"accessKey": ""
;;;;;;;;}, {
;;;;;;;;;;;;"userContextId": 3,
;;;;;;;;;;;;"public": false,
;;;;;;;;;;;;"icon": "txt",
;;;;;;;;;;;;"color": "blue",
;;;;;;;;;;;;"name": "test 3"
;;;;;;;;}
;;;;]
;}

CsScript x
x.SetOptions("references=C:\Program Files (x86)\Quick Macros 2\ChilkatDotNet47.dll")
x.AddCode("")

;call static function
str R=x.Call("Class1.TestFunction" json)
out R


#ret
//C# code
using System;
using Chilkat;

public class Class1
{
,public static string TestFunction(string s)
,{
,,Chilkat.JsonObject json = new Chilkat.JsonObject();
,,bool success = json.Load(s);
,,Chilkat.JsonArray tagsArray = json.ArrayOf("identities");
,,Console.WriteLine("Remove whole json object userContextId': 3 and output result");
,,tagsArray.DeleteAt(2);
,,json.EmitCompact = false;
,,Console.WriteLine(json.Emit());
,,Console.WriteLine("Add object and output result");
,,json.UpdateString("identities[2].userContextId","7");
,,json.UpdateBool("identities[2].public",true);
,,json.UpdateString("identities[2].icon","doc");
,,json.UpdateString("identities[2].color","red");
,,json.UpdateString("identities[2].name","test 7");
,,json.UpdateString("identities[2].accessKey","123");
,,json.EmitCompact = false;
,,Console.WriteLine(json.Emit());    
,,Console.WriteLine("Modify key-value pair, of ''userContextId'': 1,''  ---> modify name from ''test 1'' to ''test 1B''");
,,json.UpdateString("identities[0].name","test 1B");
,,json.EmitCompact = false;
,,Console.WriteLine(json.Emit());        
,,return "finished";
,}
}
#5
this is the activeX version of chilkat in qm

goto Download the Chilkat ActiveX

install 32bit version i believe

Function ChilkatActivexExamples
Code:
Copy      Help
out
typelib Chilkat_v9_5_0 {004CB902-F437-4D01-BD85-9E18836DA5C2} 1.0
Chilkat_v9_5_0.ChilkatJsonArray ja._create
Chilkat_v9_5_0.ChilkatJsonObject jo._create
Chilkat_v9_5_0.ChilkatJsonObject json._create

str filename.expandpath("$desktop$\test.json")
jo.LoadFile(filename)
jo.EmitCompact=0
out "original json with formatting"
out jo.Emit
out "Remove whole json object userContextId': 3 and output result"
ja= jo.ArrayOf("identities")
ja.DeleteAt(2)
out jo.Emit
out "Add object and output result"
ja.AddObjectAt(2)
json=jo.ArrayOf("identities").ObjectAt(2)
json.SetStringOf("userContextId","7")
json.SetBoolOf("public",1)
json.SetStringOf("icon","doc");
json.SetStringOf("color","red");
json.SetStringOf("name","test 7");
json.SetStringOf("accessKey","123");
out jo.Emit
json=jo.ArrayOf("identities").ObjectAt(0)
out "Modify key-value pair, of ''userContextId'': 1,''  ---> modify name from ''test 1'' to ''test 1B''"
json.UpdateString("name","test 1B");
out jo.Emit
jo.WriteFile(filename)
out "finished"
#6
@Kevin, Thanks! Going to try this!
#7
very cool! S


Forum Jump:


Users browsing this thread: 1 Guest(s)