Jump to content

JSON parse to variables


Recommended Posts

Hello, I have a very long json string and I want to transform it to variables. The json look like this:

{"Brayan":{"id":53192745,"name":"Brayan","profileIconId":636,"age":30,"something":1437402511000}}

I make a short version but it's always designed like this:

"name":"Value",

and I would like to parse it and get it into variables, so my above exemple become:

$name = "Value"

I get this json code from a get request inside my code, so I can't edit it on a program, I have to do it in AutoIt.

I don't really understand JSON and I see this post :  https://www.autoitscript.com/forum/topic/148114-a-non-strict-json-udf-jsmn/

But I don't rlly understand how works this part of code (because I don't understand JSON):

Local $Obj
Json_Put($Obj, ".foo", "foo")
Json_Put($Obj, ".bar[0]", "bar")
Json_Put($Obj, ".test[1].foo.bar[2].foo.bar", "Test")

Local $Test = Json_Get($Obj, '["test"][1]["foo"]["bar"][2]["foo"]["bar"]') ; "Test"

I think it would be useful for what I want to do.

I can't do something like this:

$json_string = '{"Brayan":{"id":53192745,"name":"Brayan","profileIconId":636,"age":30,"something":1437402511000}}'

Global $parsed_json_string = StringSplit($json_string, '"')

$string = StringReplace($parsed_json_string[5], ":", "");  '  :53192745,   ' -> string (without quotes)
$parsed_json_string[5] = StringReplace($string, ",", "");  '  53192745,   ' -> string (without quotes)
$id = $parsed_json_string[5]

Because my json string is much too big for coding this on every variables I want.

My idea is to get a function that can do this:

#include <MsgBoxConstants.au3>

$brayan = '{"Brayan":{"id":53192745, "age":30}}'
$x = FunctionOfMyDreams($brayan, "age")

MsgBox($MB_OK, "", $x); -> will return "30"

Hope someone know how to do this, it would be very usefull for my project :)

Edited by blackrainbow
Link to comment
Share on other sites

Or, using that UDF you pointed to and it's machine code parser:

#include "Json.au3"

$jsonString = '{"Brayan":{"id":53192745,"name":"Brayan","profileIconId":636,"age":30,"something":1437402511000}}'
$Obj = Json_Decode($jsonString)

$age = functionOfMyDreams($Obj, "Brayan", "age")

ConsoleWrite("Age: " & $age & @CRLF)

Exit

Func functionOfMyDreams($decodedObject, $targetObject, $targetKeyInObject)
    $value = Json_Get($decodedObject, '["' & $targetObject & '"]["' & $targetKeyInObject & '"]')
    return $value
EndFunc   ;==>functionOfMyDreams

 

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Ok, I test both, both works but this methode:

Or, using that UDF you pointed to and it's machine code parser:

#include "Json.au3"

$jsonString = '{"Brayan":{"id":53192745,"name":"Brayan","profileIconId":636,"age":30,"something":1437402511000}}'
$Obj = Json_Decode($jsonString)

$age = functionOfMyDreams($Obj, "Brayan", "age")

ConsoleWrite("Age: " & $age & @CRLF)

Exit

Func functionOfMyDreams($decodedObject, $targetObject, $targetKeyInObject)
    $value = Json_Get($decodedObject, '["' & $targetObject & '"]["' & $targetKeyInObject & '"]')
    return $value
EndFunc   ;==>functionOfMyDreams

 

Don't work if there is multiples times the same variables name but with differerants values, for exemple:

$jsonString = '{"Brayan":{"id":53192745,"name":"Brayan","profileIconId":636,"age":30,"something":1437402511000,"something":18975}}'

it will just return 18975, it would be great if it can return a table like:

$value[0] = 1437402511000
 $value[1] = 18975

 

Link to comment
Share on other sites

rfc7159 states that the keys should be unique. Not that json is syntactically incorrect if that isn't the case, but A.F.A.I.K. Json parsers normally just return the last matching key. But you didn't ask about that :) If you want multiple elements in an array, you can specify the element count with ' ["array_name"][<element index>] '. I believe there's also a function to get the object count. Haven't looked into it that far. But yeah, if you know what you need specifically and you'd prefer automatic array creation and less code over speed and pre-baked parsing code then regex is certainly an option as well, of course :)

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

In case of 2 variables with same name the 2nd value overwrites the 1st one... strange syntax
But regex doesn't care about the syntax accuracy   :)

#Include <Array.au3>

$str = '{"Brayan":{"id":53192745,"name":"Brayan","profileIconId":636,"age":30,"something":1437402511000,"something":18975}}'

$str = StringReplace($str, '"', "")
$res = StringRegExp($str, '(?:id|name|age|something):([^\{,}]+)', 3)
 _ArrayDisplay($res)

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...