AI Zone Admin Forum Add your forum

NEWS: Chatbots.org survey on 3000 US and UK consumers shows it is time for chatbot integration in customer service!read more..

Parsing List
 
 

{
"values": [{
"fname": "Neha",
"sname": "Prabhu",
"subjects": ["eng", "bio", "phy"]
}, {
"fname": "Pooja",
"sname": "Naik",
"subjects": ["eng", "chem", "phy"]
}]
}

How can i parse the inner list (“values”) and outer list(“subjects”) from json in most efficient way?

 

 
  [ # 1 ]

by parse do you mean get some access from script?  For an array, you can loop thru contents like this, assuming your json is in $_json variable:
@0 = ^query(direct_sv $_json values ?)
loop()
{
$_object = ^first(@0object)
... do something with this object
e.g. $_object.fname would print out that value
}

 

 
  [ # 2 ]

By parsing, i mean i want to print he values of the json object:

I want the output as “Neha Prabhu opted for the subjects eng, bio and phy”

$$tmp = ^jsonopen(GET $$url “” $$user_agent)
  @1 = query(direct_v ? fname ? 1)
  @2 = query(direct_v ? sname ? 1)
  @2object @1object opted for the subjects

  $subjects = ^jsonpath(.values.0.subjects ^jsonparse( $$tmp ))
  $no_of_subjects = ^length($subjects)
  $count = 0
  $limit = $no_of_subjects- 1
  loop( $no_of_subjects )
  {
^jsonpath(.values.0.subjects.$count ^jsonparse( $$tmp ))
$count += 1
if ($count < $limit) {, }
  if ($count == $limit ) { and }
  if ($count == $no_of_resp ) {.}
  }

I was able to do it using the above code, but instead of using jsonparse I want to use @0 = ^query(direct_sv $_json values ?), how can i do it?

 

 
  [ # 3 ]

Jsonopen automatically parses the response into JSON facts so you can reference them directly.
I prefer to not use queries and just iterate normally.

$_tmp = ^jsonopen(transient GET $$url “” $$user_agent)

$_numpeople = ^length($_tmp.values)
$_indexpeople = 0
^loop($_numpeople)
{
$_object = $_tmp.values[$_indexpeople]
$_object.fname $_object.sname opted for the subjects

$_numsubjects = ^length($_object.subjects)
$_limit = $_numsubjects - 1
$_indexsubject = 0
^loop($_numsubjects)
{
$_object.subjects[$_indexsubject]
if ($_indexsubject < $_limit) { , }
    else if ($_indexsubject == $_limit) { and }
    else { . }
    $_indexsubject += 1
  }

  $_indexpeople += 1
}

$_tmp.

 

 
  [ # 4 ]

Thanks a lot Andy Heydon. This was very helpful

 

 
  login or register to react