Accessing a decoded json string value in twig - json

I am working on a Symfony project and need to loop through data to populate saved fields in a form (specifically the number of bed types in a property). I save the data in the database table as a json string, as follows:
{"2":"5","3":"0","4":"0","5":"0","6":"0","7":"0"}
This JSON follows the structure "BEDID": NUMBER OF BEDS. I implemented the solution regarding decoding json in twig as stated here https://stackoverflow.com/a/14504988/5194337 but I am having trouble actually being able to access each specific value in the decoded json. I use this in the twig template to decode the json (based on the fact my data is stored in a variable called specifics and website.id references one of multiple websites owned by the user:
{% set beds = specifics[website.id].0.standardBedTypes|json_decode %}
So, once I do this, I try and access the value of each number of beds as follows:
{{ beds[standard_bed.id] }}
standard_bed being the value in the for loop. But, when I load the page I get the following error:
Impossible to access a key "2" on an object of class "stdClass" that
does not implement ArrayAccess interface.
I guess this means that the decoded value of the json is technically not an array, but I cannot think of any other method of referencing each value, so help with this is appreciated.

From the documentation here, you can pass it as an option. See the options here.
You want to pass JSON_OBJECT_AS_ARRAY
Decodes JSON objects as PHP array.
So basically you want to do:
{% set beds = specifics[website.id].0.standardBedTypes|json_decode(constant('JSON_OBJECT_AS_ARRAY')) %}
If you want to access to objects properties, you can do it using the attribute function.

Related

Extract a specified data object that resides inside a PDF form field to an external file

I am looking for a script to extract a data object nested inside a data object to a new data object. Aside from being able to easily clone the object by creating a new one, while I can also extract one of several objects nested inside an object that includes all its related properties and values, the name associated with the property and values is missing. Hence, I require a script to create a new object containing any one of several names including its related properties and values extracted from the existing object provided below.
{"J Doe Company":{"lastUpdate":"01/05/2023","website":"jdoecompany.com","userID":"jdoe1985#gmail.com","password":"igfndhsi1985","primaryCC":"Discover","secondaryCC":"Capital One","primaryBank":"Chase","secondaryBank":"","sq1":"Year Graduated HS","sa1":"1985","sq2":"","sa2":"","notes1":"Sample password record","notes2":""},"Bob The Builder":{"lastUpdate":"01/05/2023","website":"bobthebuilder.com","userID":"bobthebuilder#gmail.com","password":"bbob1985","primaryCC":"Amazon Visa","secondaryCC":"Mastercard","primaryBank":"Wells Fargo","secondaryBank":"","sq1":"First Girlfriend's Name","sa1":"Kaye","sq2":"","sa2":"","notes1":"Sample password record","notes2":""},"SpongeBob Square Pants":{"lastUpdate":"01/07/2023","website":"spongebobsquarepants.com","userID":"spongebob#gmail.com","password":"spongebob1999","primaryCC":"None/Not Applicable","secondaryCC":"","primaryBank":"None/Not Applicable","secondaryBank":"","sq1":"Year show debuted on TV","sa1":"1999","sq2":"","sa2":"","notes1":"Animated TV show for kids","notes2":""}}
You can run cpdf -output-json in.pdf -o put.json and process the result. The format is described in the cpdf manual.
Upon further trial and error after learning more about how to create and edit an object literal, the script required to add an object along with its key-value properties is as follows:
dsFld =getField("dataSrc");// dataSRC is a hidden field containing a JS object converted to a JSON string
oVendors = JSON.parse(dsFld.value); // oVendors is a JS object
oVendors[event.value]=oVendorsPropValues;//oVendorsPropValues = {key-value pairs}
dsFld.value = JSON.stringify(oVendors);// convert the JS obj back to a JSON string

Using Flow to return a SharePoint list to Powerapps

I've used this Flow to return the count of any Sharepoint list to Powerapps.
https://masteroffice365.com/get-sharepoint-library-or-list-total-items-from-power-apps/
How would I modify it to return the contents of a list to Powerapps, so that I can use Powerapps to put it into a collection?
Would this mean I don't have to worry about Delegation if the list has more than 2000 items?
This is what I've tried so far.
There is a variable TotalItemsCount which I have changed to ListItems. Instead of using an Integer I set ListItems to an array.
In the Get Library list contents I use this for the URI.
concat( '_api/web/lists/GetbyTitle(''', first( body('Filter_Library_List_Being_Queried') )?['displayName'], ''')/Items' )
I'm not sure what to put in as the last step given that I want it to be able to return the contents of any list. I think this rules out a parse json step as that requires a definite schema.
I've added an ApplyToEach
I'm getting this error message when it runs.
ExpressionEvaluationFailed. The execution of template action
'Apply_to_each' failed: the result of the evaluation of 'foreach'
expression '#body('Get_Library_List_Contents')' is of type 'Object'.
The result must be a valid array.
I don't think you can return an array back to PowerApps. You would have to return the response as a JSON string, then have your PowerApp do the logic to convert the JSON string into a collection.
Likely your PowerApp would have to include something like this to convert the JSON string that's returned from the flow:
ClearCollect(
*collectionName*,
MatchAll(
*JSON_String*,
*"\{""date"":""(?<date>[^""]*)"",""message"":""(?<message>[^""]*)"",""user"":""(?<user>[^""]*)""\}"*
));
Flow returning response body to PowerApp
If I understand you correctly, you want to retrieve 14000 records from the Sharepoint list, and not just the total count.
Would this mean I don't have to worry about Delegation if the list has more than 2000 items?
Yes, when you use a cloud flow rather than directly accessing Sharepoint list from Powerapps, you basically avoid delegation of 2k records.
Now coming back to you main topic of retrieving Records, you would have to Test and run your flow and check what does the below http return. I believe it returns a JSON Array.
concat( '_api/web/lists/GetbyTitle(''', first( body('Filter_Library_List_Being_Queried') )?['displayName'], ''')/Items' )
You would have to apply a for each or clean your JSON output to return String Array or JSON Array as output of your all 14K Records.
In addition if you are using Sharepoint online why not use connector for flow mentioned here

Azure | ADF | How to use a String variable to lookup a Key in an Object type Parameter and retrieve its Value

I am using Azure Data Factory. I'm trying to use a String variable to lookup a Key in a JSON array and retrieve its Value. I can't seem to figure out how to do this in ADF.
Details:
I have defined a Pipeline Parameter named "obj", type "Object" and content:
{"values":{"key1":"value1","key2":"value2"}}
Parameter definition
I need to use this pipeline to find a value named "key1" and return it as "value1"; "key2" and return it as "value2"... and so on. I'm planning to use my "obj" as a dictionary, to accomplish this.
Technically speaking, If i want to find the value for key2, I can use the code below, and it will be returned "value2":
#pipeline().parameters.obj.values.key2
What i can't figure out is how to do it using a variable (instead of hardcoded "key2").
To clear things out: I have a for-loop and, inside it, i have just a copy activity: for-each contents
The purpose of the copy activity is to copy the file named item().name, but save it in ADLS as whatever item().name translates to, according to "obj"
This is how the for-loop could be built, using Python: python-for-loop
In ADF, I tried a lot of things (using concat, replace...), but none worked. The simpliest woult be this:
#pipeline().parameters.obj.values.item().name
but it throws the following error:
{"code":"BadRequest","message":"ErrorCode=InvalidTemplate, ErrorMessage=Unable to parse expression 'pipeline().parameters.obj.values.item().name'","target":"pipeline/name_of_the_pipeline/runid/run_id","details":null,"error":null}
So, can you please give any ideas how to define my expression?
I feel this must be really obvious, but I'm not getting there.....
Thanks.
Hello fellow Pythonista!
The solution in ADF is actually to reference just as you would in Python by enclosing the 'variable' in square brackets.
I created a pipeline with a parameter obj like yours
and, as a demo, the pipeline has a single Set Variable activity that got the value for key2 into a variable.
This is documented but you need X-ray vision to spot it here.
Based on your comments, this is the output of a Filter activity. The Filter activity's output is an object that contains an array named value, so you need to iterate over the "output.value":
Inside the ForEach you reference the name of the item using "item().name":
EDIT BASED ON MORE INFORMATION:
The task is to now take the #item().name value and use it as a dynamic property name against a JSON array. This is a bit of a challenge given the limited nature of the Pipeline Expression Language (PEL). Array elements in PEL can only be referenced by their index value, so to do this kind of complex lookup you will need to loop over the array and do some string parsing. Since you are already inside a FOR loop, and nested FOR loops are not supported, you will need to execute another pipeline to handle this process AND the Copy activity. Warning: this gets ugly, but works.
Child Pipeline
Define a pipeline with two parameters, one for the values array and one for the item().name:
When you execute the child pipeline, pass #pipeline.parameters.obj.values as "valuesArray" and #item().name as "keyValue".
You will need several string parsing operations, so create some string variables in the Pipeline:
In the Child Pipeline, add a ForEach activity. Check the Sequential box and set the Items to the valuesArray parameter:
Inside the ForEach, start by cleaning up the current item and storing it as a variable to make it a little easier to consume.
Parse the object key out of the variable [this is where it starts to get a little ugly]:
Add an IF condition to test the value of the current key to the keyValue parameter:
Add an activity to the TRUE condition that parses the value into a variable [gets really ugly here]:
Meanwhile, back at the Pipeline
At this point, after the ForEach, you will have a variable (IterationValue) that contains the correct value from your original array:
Now that you have this value, you can use that variable as a DataSet parameter in the Copy activity.

What does it mean when a KinematicBody2D is stored in a JSON file - Godot?

After writing a few files for saving in my JSON file in Godot. I saved the information in a variable called LData and it is working. LData looks like this:
{
"ingredients":[
"[KinematicBody2D:1370]"
],
"collected":[
{
"iname":"Pineapple",
"collected":true
},{
"iname":"Banana",
"collected":false
}
]
}
What does it mean when the file says KinematicBody2D:1370? I understand that it is saving the node in the file - or is it just saving a string? Is it saving the node's properties as well?
When I tried retrieving data - a variable that is assigned to the saved KinematicBody2D.
Code:
for ingredient in LData.ingredients:
print(ingredient.iname)
Error:
Invalid get index name 'iname' (on base: 'String')
I am assuming that the data is stored as a String and I need to put some code to get the exact node it saved. Using get_node is also throwing an error.
Code:
for ingredient in LData.ingredients:
print(get_node(ingredient).iname)
Error:
Invalid get index 'iname' (on base: 'null instance')
What information is it exactly storing when it says [KinematicBody2D:1370]? How do I access the variable iname and any other variables - variables that are assigned to the node when the game is loaded - and is not changed through the entire game?
[KinematicBody2D:1370] is just the string representation of a Node, which comes from Object.to_string:
Returns a String representing the object. If not overridden, defaults to "[ClassName:RID]".
If you truly want to serialize an entire Object, you could use Marshalls.variant_to_base64 and put that string in your json file. However, this will likely bloat your json file and contain much more information than you actually need to save a game. Do you really need to save an entire KinematicBody, or can you figure out the few properties that need to be saved (postion, type of object, ect.) and reconstruct the rest at runtime?
You can also save objects as Resources, which is more powerful and flexible than a JSON file, but tends to be better suited to game assets than save games. However, you could read the Resource docs and see if saving Resources seems like a more appropriate solution to you.

Neo4j node property containing raw json as metadata

Is this possible to have a node property as json raw string and to filter on it with cypher ?
I have a node with some defined properties and metadata (json raw string).
I would like to select or filter on those metadata property.
This is something like this :
START movie=node:TYPE_INDEX(Type = 'MOVIE') // Start with the reference
MATCH movie-[t:TAG]->tag
WHERE collect(movie.Metadata).RatingPress > 3
RETURN distinct movie.Label
And metadata are something like this :
{"RatingPress" : "0","RatingSpectator" : 3"}
I have expected to use collect function in order to call the property like this :
collect(movie.Metadata).RatingPress
But, of course it fails...
Is this a way to bind some json string from a node property with cypher ?
Thanks for your help
That's going against the principles of properties. Why not set the properties in the JSON metadata directly on the node?
But to answer your question:
No, cypher has no knowledge about JSON.
We treat the entire Node as a JSON blob. Since Neo4j doesn't support hierarchical properties, we flatten out the JSON into delimited property names on save and unflatten them on read. You can then form Cypher queries on (for example) property name "foo.bar.baz". The queries tend to look a bit funky because you'll need to quote them using single back quotes, but it works.