Pass string to parse it as JSONARRAY

Hello,

I’m trying to parse string to JSONARRAY. It contains array of JSONOBJECT but running into parsing error. I’m not sure if it’s an issue with my syntax or if it is not allowed to pass array of JSONOBJECT in JSONARRAY string. Can someone please please help with this issue or any work around for this structure?

eg.,
STRING str = “[{“keyA”:”%test1%",“keyB”:“test1”},{“keyA”:"%test2%",“keyB”:“test2”}]";
JSONARRAY jsonA = parse_json_array(str);

Output:
Runtime Error: “[{“keyA”:”%test1%",“keyB”:“test1”},{“keyA”:"%test2%",“keyB”:“test2”}]" cannot be parsed as a json array.

@dhruva A run-time error can occur if the input string cannot be converted into a JSON object or a JSON array. To be properly formatted, besides having the proper nesting and matching of curly braces { } and brackets [ ], each value field must be one of the following: a string (in double quotes "), a number, a boolean ( true or false ), or a JSONOBJECT or JSONARRAY. Each key of a key:value pair must be a string in double quotes.

Try using \ instead of %

GSQL > RUN QUERY jsonEx("[123]","{\"abc\":123}")
or curl -X GET 'http://localhost:9000/query/jsonEx?strA=\[123\]&strB=\{"abc":123\}' 
{
  "error": false,
  "message": "",
  "version": {
    "edition": "developer",
    "schema": 0,
    "api": "v2"
  },
  "results": [{
    "jsonA": [123],
    "jsonO": {"abc": 123}
  }]
}

https://docs.tigergraph.com/dev/gsql-ref/querying/operators-functions-and-expressions#jsonobject-and-jsonarray-functions

@Jon_Herke Tyty for the response :slight_smile: I read that part of the documentation and was following those steps but I need help with the jsonarray parsable string formation. I’m trying following string which is of format [jsonobject - {keyA: “”, keyB: “”}] but it’s giving parsing error. My questions is around jsonarray can only parse string of format [int], [int, string], etc ? or does it support complex structure like [jsonobject]

> str_1 = "[{\"keyA\":\"%test1%\",\"keyB\":\"test1\"},{\"keyA\":\"%test2%\",\"keyB\":\"test2\"}]"
> str_2 = "[\"{\"keyA\":\"%test1%\",\"keyB\":\"test1\"}\",\"{\"keyA\":\"%test2%\",\"keyB\":\"test2\"}\"]"

I tried those 2 strings within the query but when I use parse_json_array, it give run time error.

Update: I was able to resolve this issue by using combination of double and single quotes. (my bad) It was getting confused with how I was using the double quotes for 2 separate reasons.

Solution:

  1. When used within the query:

STRING str = "[\"{'keyA':'%test1%','keyB':'test1'}\",\"{'keyA':'%test2%','keyB':'test2'}\"]";

  1. When sent in GraphStudio params:

["{‘keyA’:’%test1%’,‘keyB’:‘test1’}","{‘keyA’:’%test2%’,‘keyB’:‘test2’}"]

You can use double quotes everywhere, you just need to escape excessively:

CREATE QUERY test3(/* Parameters here */) FOR GRAPH test { 
  
  STRING str = "[{\"keyA\":\"%test1%\",\"keyB\":\"test1\"},{\"keyA\":\"%test2%\",\"keyB\":\"test2\"}]";
  JSONARRAY jsonA;
  jsonA = parse_json_array(str);
  PRINT jsonA; 
}

This installs, and outputs proper JSON:

[
  {
    "jsonA": [
      {
        "keyA": "%test1%",
        "keyB": "test1"
      },
      {
        "keyA": "%test2%",
        "keyB": "test2"
      }
    ]
  }
]
2 Likes