I'm facing a problem when trying to get the id of one group in Ansible tower using a playbook and inside the playbook I am using the tower-cli command.
The problem is that when I get the output of this tower-cli command:
tower-cli group list -i {{ inventory_id }} --name {{ get_specific_group_hosts }} -f json -a
I register this in one variable group_id and then I tried to get the id from the output which is a JSON.
{
"stderr_lines": [
"/var/lib/awx/venv/ansible_ibm/lib/python2.7/site-packages/OpenSSL/crypto.py:12: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in the next release.",
" from cryptography import x509"
],
"changed": true,
"end": "2022-01-28 15:54:34.901091",
"_ansible_no_log": false,
"_ansible_delegated_vars": {},
"cmd": "tower-cli group list -i 1283 --name grp_infrastructure -f json -a",
"stdout": "{\n \"count\": 1, \n \"next\": null, \n \"previous\": null, \n \"results\": [\n {\n \"id\": 6707, \n \"type\": \"group\", \n \"url\": \"/api/v2/groups/6707/\", \n \"related\": {\n \"created_by\": \"/api/v2/users/1338/\", \n \"modified_by\": \"/api/v2/users/1338/\", \n \"variable_data\": \"/api/v2/groups/6707/variable_data/\", \n \"hosts\": \"/api/v2/groups/6707/hosts/\", \n \"potential_children\": \"/api/v2/groups/6707/potential_children/\", \n \"children\": \"/api/v2/groups/6707/children/\", \n \"all_hosts\": \"/api/v2/groups/6707/all_hosts/\", \n \"job_events\": \"/api/v2/groups/6707/job_events/\", \n \"job_host_summaries\": \"/api/v2/groups/6707/job_host_summaries/\", \n \"activity_stream\": \"/api/v2/groups/6707/activity_stream/\", \n \"inventory_sources\": \"/api/v2/groups/6707/inventory_sources/\", \n \"ad_hoc_commands\": \"/api/v2/groups/6707/ad_hoc_commands/\", \n \"inventory\": \"/api/v2/inventories/1283/\"\n }, \n \"summary_fields\": {\n \"inventory\": {\n \"id\": 1283, \n \"name\": \"inventory\", \n \"description\": \"inventory\", \n \"has_active_failures\": true, \n \"total_hosts\": 562, \n \"hosts_with_active_failures\": 68, \n \"total_groups\": 30, \n \"has_inventory_sources\": false, \n \"total_inventory_sources\": 0, \n \"inventory_sources_with_failures\": 0, \n \"organization_id\": 142, \n \"kind\": \"\"\n }, \n \"created_by\": {\n \"id\": 1338, \n \"username\": \"\", \n \"first_name\": \"\", \n \"last_name\": \"\"\n }, \n \"modified_by\": {\n \"id\": 1338, \n \"username\": \"\", \n \"first_name\": \"\", \n \"last_name\": \"\"\n }, \n \"user_capabilities\": {\n \"edit\": true, \n \"delete\": true, \n \"copy\": true\n }\n }, \n \"created\": \"2020-09-17T10:47:16.910421Z\", \n \"modified\": \"2020-09-17T10:47:16.910437Z\", \n \"name\": \"grp_infrastructure\", \n \"description\": \"\", \n \"inventory\": 1283, \n \"variables\": \"\"\n }\n ]\n}",
"start": "2022-01-28 15:54:34.289617",
"delta": "0:00:00.611474",
"stderr": "/var/lib/awx/venv/ansible_ibm/lib/python2.7/site-packages/OpenSSL/crypto.py:12: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in the next release.\n from cryptography import x509",
"rc": 0,
"invocation": {
"module_args": {
"warn": true,
"executable": null,
"_uses_shell": true,
"strip_empty_ends": true,
"_raw_params": "tower-cli group list -i 1283 --name grp_infrastructure -f json -a",
"removes": null,
"argv": null,
"creates": null,
"chdir": null,
"stdin_add_newline": true,
"stdin": null
}
},
"stdout_lines": [
"{",
" \"count\": 1, ",
" \"next\": null, ",
" \"previous\": null, ",
" \"results\": [",
" {",
" \"id\": 6707, ",
" \"type\": \"group\", ",
" \"url\": \"/api/v2/groups/6707/\", ",
" \"related\": {",
" \"created_by\": \"/api/v2/users/1338/\", ",
" \"modified_by\": \"/api/v2/users/1338/\", ",
" \"variable_data\": \"/api/v2/groups/6707/variable_data/\", ",
" \"hosts\": \"/api/v2/groups/6707/hosts/\", ",
" \"potential_children\": \"/api/v2/groups/6707/potential_children/\", ",
" \"children\": \"/api/v2/groups/6707/children/\", ",
" \"all_hosts\": \"/api/v2/groups/6707/all_hosts/\", ",
" \"job_events\": \"/api/v2/groups/6707/job_events/\", ",
" \"job_host_summaries\": \"/api/v2/groups/6707/job_host_summaries/\", ",
" \"activity_stream\": \"/api/v2/groups/6707/activity_stream/\", ",
" \"inventory_sources\": \"/api/v2/groups/6707/inventory_sources/\", ",
" \"ad_hoc_commands\": \"/api/v2/groups/6707/ad_hoc_commands/\", ",
" \"inventory\": \"/api/v2/inventories/1283/\"",
" }, ",
" \"summary_fields\": {",
" \"inventory\": {",
" \"id\": 1283, ",
" \"name\": \"inventory\", ",
" \"description\": \"inventory\", ",
" \"has_active_failures\": true, ",
" \"total_hosts\": 562, ",
" \"hosts_with_active_failures\": 68, ",
" \"total_groups\": 30, ",
" \"has_inventory_sources\": false, ",
" \"total_inventory_sources\": 0, ",
" \"inventory_sources_with_failures\": 0, ",
" \"organization_id\": 142, ",
" \"kind\": \"\"",
" }, ",
" \"created_by\": {",
" \"id\": 1338, ",
" \"username\": \"\", ",
" \"first_name\": \"\", ",
" \"last_name\": \"\"",
" }, ",
" \"modified_by\": {",
" \"id\": 1338, ",
" \"username\": \"\", ",
" \"first_name\": \"\", ",
" \"last_name\": \"\"",
" }, ",
" \"user_capabilities\": {",
" \"edit\": true, ",
" \"delete\": true, ",
" \"copy\": true",
" }",
" }, ",
" \"created\": \"2020-09-17T10:47:16.910421Z\", ",
" \"modified\": \"2020-09-17T10:47:16.910437Z\", ",
" \"name\": \"grp_infrastructure\", ",
" \"description\": \"\", ",
" \"inventory\": 1283, ",
" \"variables\": \"\"",
" }",
" ]",
"}"
]
}
So when I used different types of JSON strings and methods nothing worked, sometimes I get an error and other times I get an empty string like when I tried this:
- name: "get the json list"
debug:
msg: "{{ group_id | json_query('stdout.results.id') }}"
The output is like this`:
TASK [get the json list] *****************************************************************
ok: [localhost] => {
"msg": ""
}
This is because group_id.stdout is a string representing a JSON, not a JSON.
You will have to pass it through the from_json filter.
Also, you don't need a JMESPath expression and a json_query for such a simple requirement, you can use map for this.
So, your task should end up being
- debug:
var: (group_id.stdout | from_json).results | map(attribute='id')
Given the playbook:
- hosts: localhost
gather_facts: no
tasks:
- debug:
var: (group_id.stdout | from_json).results | map(attribute='id')
vars:
group_id:
stdout: "{\n \"count\": 1, \n \"next\": null, \n \"previous\": null, \n \"results\": [\n {\n \"id\": 6707, \n \"type\": \"group\", \n \"url\": \"/api/v2/groups/6707/\", \n \"related\": {\n \"created_by\": \"/api/v2/users/1338/\", \n \"modified_by\": \"/api/v2/users/1338/\", \n \"variable_data\": \"/api/v2/groups/6707/variable_data/\", \n \"hosts\": \"/api/v2/groups/6707/hosts/\", \n \"potential_children\": \"/api/v2/groups/6707/potential_children/\", \n \"children\": \"/api/v2/groups/6707/children/\", \n \"all_hosts\": \"/api/v2/groups/6707/all_hosts/\", \n \"job_events\": \"/api/v2/groups/6707/job_events/\", \n \"job_host_summaries\": \"/api/v2/groups/6707/job_host_summaries/\", \n \"activity_stream\": \"/api/v2/groups/6707/activity_stream/\", \n \"inventory_sources\": \"/api/v2/groups/6707/inventory_sources/\", \n \"ad_hoc_commands\": \"/api/v2/groups/6707/ad_hoc_commands/\", \n \"inventory\": \"/api/v2/inventories/1283/\"\n }, \n \"summary_fields\": {\n \"inventory\": {\n \"id\": 1283, \n \"name\": \"inventory\", \n \"description\": \"inventory\", \n \"has_active_failures\": true, \n \"total_hosts\": 562, \n \"hosts_with_active_failures\": 68, \n \"total_groups\": 30, \n \"has_inventory_sources\": false, \n \"total_inventory_sources\": 0, \n \"inventory_sources_with_failures\": 0, \n \"organization_id\": 142, \n \"kind\": \"\"\n }, \n \"created_by\": {\n \"id\": 1338, \n \"username\": \"\", \n \"first_name\": \"\", \n \"last_name\": \"\"\n }, \n \"modified_by\": {\n \"id\": 1338, \n \"username\": \"\", \n \"first_name\": \"\", \n \"last_name\": \"\"\n }, \n \"user_capabilities\": {\n \"edit\": true, \n \"delete\": true, \n \"copy\": true\n }\n }, \n \"created\": \"2020-09-17T10:47:16.910421Z\", \n \"modified\": \"2020-09-17T10:47:16.910437Z\", \n \"name\": \"grp_infrastructure\", \n \"description\": \"\", \n \"inventory\": 1283, \n \"variables\": \"\"\n }\n ]\n}"
This yields:
TASK [debug] *************************************************************
ok: [localhost] =>
(group_id.stdout | from_json).results | map(attribute='id'):
- 6707
Related
Following is the output from an ansible task
"msg": [
"Executing script ment.sh with following arguments",
"Arguments passed : userName=root hostName=007 passWord=123 payload=/tmp/007.json.19.18 adminpass=abc",
"spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root#007",
"Warning: Permanently added '007' (ECDSA) to the list of known hosts.",
"",
"",
"root#007's password: ",
"Last login: Thu Feb 9 17:52:48 2023 from 1.1.1.1",
"",
"\u001b]0;root#oak:~\u0007\u001b[?1034h[root#oak ~]# ",
"Second try on password",
"createdocker -j -r /tmp/json.19.18",
"Enter an initial password for Web Console account (admin):",
"Retype the password for Web Console account (admin):",
"User 'admin' created successfully...",
"{",
" \"jobId\" : \"dd370bce-03d7-4436-93b4-9a9ed7658394\",",
" \"status\" : \"Created\",",
" \"message\" : null,",
" \"reports\" : [ ],",
" \"createTimestamp\" : \"February 09, 2023 17:53:29 PM UTC\",",
" \"resourceList\" : [ ],",
" \"description\" : \"Provisioning service creation\",",
" \"updatedTime\" : \"February 09, 2023 17:53:29 PM UTC\",",
" \"jobType\" : null",
"}",
"",
"Successfully created provisioning job",
"exit",
"\u001b]0;root#oak:~\u0007[root#oak ~]# exit",
"logout",
"Connection to closed."
]
Can someone please help me get the jobId value i.e. "dd370bce-03d7-4436-93b4-9a9ed7658394" into a variable
I got it:
- name: Store jobId in a variable
debug:
msg: "{{ the_output | select('match','^.*\"jobId\"\\s*:\\s*\"(.*)\".*$') | regex_replace('jobId\" :', '') | regex_replace('[^a-zA-Z0-9-]', '') }}"
Ansible returns:
dd370bce-03d7-4436-93b4-9a9ed7658394
I would like to FIlter value of Endpoint from stdout_lines of Ansible output below:
"{",
" \"DBCluster\": {",
" \"MasterUsername\": \"mstrusr\", ",
" \"ReaderEndpoint\": \"tstng4.cluster-ro-czzpyhxnqlzh.us-east-1.docdb.amazonaws.com\", ",
" \"VpcSecurityGroups\": [",
" {",
" \"Status\": \"active\", ",
" \"VpcSecurityGroupId\": \"sg-xxx5677\"",
" }",
" ], ",
" \"HostedZoneId\": \"ZNKXH85TT8WVW\", ",
" \"Status\": \"creating\", ",
" \"MultiAZ\": false, ",
" \"PreferredBackupWindow\": \"03:51-04:21\", ",
" \"DBSubnetGroup\": \"subnet\", ",
" \"BackupRetentionPeriod\": 1, ",
" \"PreferredMaintenanceWindow\": \"mon:05:00-mon:05:30\", ",
" \"Engine\": \"docdb\", ",
" \"Endpoint\": \"tstng4.cluster-czzpyhxnqlzh.us-east-1.docdb.amazonaws.com\", ",
" \"ClusterCreateTime\": \"2020-01-23T10:06:44.338Z\", ",
" \"EngineVersion\": \"3.6.0\", ",
" \"DeletionProtection\": true, ",
" \"DBClusterIdentifier\": \"vce-docdb-tst4\", ",
" \"DbClusterResourceId\": \"cluster-E3ICGSCJRPWDY7NRQTELXFCKTQ\", ",
" \"DBClusterMembers\": [], ",
" \"Port\": 27017, ",
" \"StorageEncrypted\": false, ",
" \"AssociatedRoles\": [], ",
" \"DBClusterParameterGroup\": \"default.docdb3.6\", ",
" \"AvailabilityZones\": [",
" \"us-east-1a\", ",
" \"us-east-1b\", ",
" \"us-east-1c\"",
" ], ",
" \"DBClusterArn\": \"arn:aws:rds:us-east-1:570346948435:cluster:vce-docdb-tst4\"",
" }",
I am trying to use below filter to get value of Endpoint:
stdout_lines.DBCluster.Endpoint
Your result is the json object which have to be piped through from_json filter. Simple test:
- hosts: masters
gather_facts: false
tasks:
- shell: /root/ansible/test.sh
register: result
- debug:
msg: "{{ (result.stdout | from_json).DBCluster.Endpoint }}"
Test data:
#!/bin/bash
echo '{'
echo '"DBCluster": {'
echo '"Endpoint": "value1"'
echo '}, "key2": "value2"'
echo '}'
I'm trying to minify some inline JSON as part of my HTML minifier. How do I make this:
> {"#context": "http://schema.org", "#type": "WebSite"} <
Into this:
>{ "#context": "http://schema.org", "#type": "WebSite" }<
I've tried gsub[/\", \s+\"/, ", "], gsub[/"\"}"/, "\" }"] and gsub[/"\"}"/, "\" }"] but that errors out.
syntax error, unexpected [, expecting ']' (SyntaxError)
[/"\"}"/, "\" }"]
^
syntax error, unexpected ',', expecting keyword_end
syntax error, unexpected ',', expecting keyword_end
syntax error, unexpected ']', expecting keyword_end
UPDATE
I've now also tried these but to no good:
[/>\s+{/, ">{ "] # > { => >{
[/}\s+>/, " }<"] # } < => }<
[%r/{"/, '{ %r/"'] # {" => { "
[%r/"}/, '%r/" }'] # "} => " }
[%r/",\s+/, ", "] # , " => , "
Resulting in:
syntax error, unexpected [, expecting ']' (SyntaxError)
[/}\s+>/, " }<"] # } < => }<
^
I would suggest a different approach:
require JSON
str = '{"#context": "http://schema.org", "#type": "WebSite"}'
new_str = JSON.parse(str).to_json
puts new_str
> {"#context":"http://schema.org","#type":"WebSite"}
Use a %r Regexp literal to escape all characters but one :
a = '{"#context": "http://schema.org", "#type": "WebSite"}'
a.gsub(%r/{"/, '{ "').gsub(%r/"}/, '" }').gsub(/\s+/, ' ')
#=> { "#context": "http://schema.org", "#type": "WebSite" }
using %r{ ... } escapes all characters but { and },same goes for /, (, etc ...
credit : Escaping '“' with regular double quotes using Ruby regex
It will escapes double quotes
gsub(/("|")/, 34.chr)
I want to split the following data by comma except the comma in double quote string
"(CONTRACTS OF 5,000 BUSHELS)"
How to do it ?
raw_str.split doesn't work
raw_str = '"WHEAT-SRW " ,150106 ,
CBT ,00 ,001 ,
31.5, 26.4, "(CONTRACTS OF 5,000 BUSHELS)"'
Actually I want to parse the file from "http://www.cftc.gov/dea/newcot/f_disagg.txt"
html = Curl.get("http://www.cftc.gov/dea/newcot/f_disagg.txt")
html.body_str.split ~~~~
Why reïnvent the wheel ?
Your text is CSV, so use the CSV gem like this
require 'csv'
CSV.foreach("f_disagg.txt", {:headers=>false, :col_sep => ",", force_quotes: false, :quote_char => "\x00"}) do |row|
p row
end
["\"WHEAT-SRW - CHICAGO BOARD OF TRADE\" ", "150106 ", "2015-01-06", "001602 ", "CBT ", "00 ", "001 ", " 375872", " 32141", " 138392", " 111530", " 5662", " 14877", " 92842", " 75279", " 34611", " 34276", " 44861", " 22692", " 342969", " 336374", " 32903", " 39498", " 260179", " 19786", " 85222", " 93057", " 6641", " 1023", " 67360", " 78720", " 16703", " 36845", " 42816", " 4110", " 238884", " 235235", " 21295", " 24944", " 115693", " 12355", " 53170", " 21065", " 1613", " 11262", " 34424", " 5501", " 8966", " 5915", " 10529", " 10098", " 104085", " 101139", " 11608", " 14554", " 3895", " -1034", " -7666", " -763", " 225", " 312", " -4547", " 2110", " 6077", " -334", " -435", " 1750", " 1461", " 2373", " 2434", " 1522", " 100.0", " 8.6", " 36.8", " 29.7", " 1.5", " 4.0", " 24.7", " 20.0", " 9.2", " 9.1", " 11.9", " 6.0", " 91.2", " 89.5", " 8.8", " 10.5", " 100.0", " 7.6", " 32.8", " 35.8", " 2.6", " 0.4", " 25.9", " 30.3", " 6.4", " 14.2", " 16.5", " 1.6", " 91.8", " 90.4", " 8.2", " 9.6", " 100.0", " 10.7", " 46.0", " 18.2", " 1.4", " 9.7", " 29.8", " 4.8", " 7.7", " 5.1", " 9.1", " 8.7", " 90.0", " 87.4", " 10.0", " 12.6", " 353", " 63", " 84", " 21", " 5", " 16", " 69", " 35", " 43", " 45", " 66", " 67", " 269", " 261", " 340", " 45", " 79", " 21", " 5", " .", " 66", " 33", " 31", " 54", " 63", " 38", " 215", " 225", " 222", " 37", " 68", " 17", " 6", " 14", " 33", " 13", " 12", " 23", " 46", " 29", " 144", " 161", " 16.2", " 12.2", " 26.0", " 21.6", " 14.6", " 10.9", " 24.0", " 18.5", " 18.4", " 14.9", " 30.4", " 25.7", " 18.2", " 14.9", " 29.9", " 25.6", " 23.6", " 19.4", " 37.6", " 32.5", " 21.7", " 17.7", " 31.5", " 26.4", "\"(CONTRACTS OF 5", "000 BUSHELS)\" ", "\"001602\" ", "\"CBT\" ", "\"001\" ", "\"A10\" ", "\"FutOnly\""], etc
EDIT: here a version that deals with the comma between parentheses, if a ) is found then the field is joined with the precious and the field it self deleted
CSV.foreach("f_disagg.txt", {:headers=>false, :col_sep => ",", force_quotes: false, :quote_char => "\x00"}) do |row|
row.each_with_index do |val, index|
if val[')']
row[index-1] = "#{row[index-1]},#{val}"
row.delete_at(index)
val = ''
end
val
end
p row
end
You can accomplish this using regex and "negative lookahead" and "negative lookbehind"[0]:
raw_str = '"WHEAT-SRW " ,150106 ,
CBT ,00 ,001 ,
31.5, 26.4, "(CONTRACTS OF 5,000 BUSHELS)"'
raw_str.split(/(,)(?<!")(!?=")/)
["\"WHEAT-SRW \" ,150106 ,2015-01-06,001602 \n ,CBT ,00 ,001 , 375872, 32141,
14.9, \n37.6, 32.5, 21.7, 17.7, \n31.5, 26.4,
\"(CONTRACTS OF 5,000 BUSHELS)\""]
You're saying: "put the comma in a capture group, only if it wasn't preceded by a double quote and if it's not followed by a double quote."
[0] http://ruby-doc.org/core-2.2.0/Regexp.html#class-Regexp-label-Anchors
here I am developing an application that retrieve data in a server via jquery ajax with then I do the Databindings with knockout
The data json well pass to my scripts the only the problem is that in my html table only one record appears
here my json as given by the server:
Json returned by the server:
{
"Id": "4 ",
" key_0 ": " 4 ",
" Nom_Agent ": " Grace ",
" key_1 ": " Grace ",
" PNom_Agent ": " Malulu ",
" key_2 ": " Malulu "
" Sexe_Agent ",
" M ",
" key_3 ",
" M ",
" Adresse_Agent ",
" 2 ",
" key_4 ",
" 2 ",
" Telephone_Agent ": " 243 900 100 115 ",
" key_5 ": " 243 900 100 115 "
" Libelle_Role ": " Cashier ",
" key_6 ": " Cashier ",
" Email_Agent ": " gracemalulu#gmail.com ",
" key_7 ": " gracemalulu#gmail.com ",
" Nom_Agence ": " KIN 02 "
" key_8 ": " 02 KIN ",
" Libelle_Caisse ": "",
" key_9 ": "",
" Id_Agence ",
" 2",
" key_10 ",
" 2",
" role_id ",
" 2",
" key_11 "
" 2",
" Id_Caisse ": " ",
" key_12 ": "",
"Password ": " 7f59e02e7deaa6a33004b25a86024dee ",
" key_13 ": " 7f59e02e7deaa6a33004b25a86024dee ",
" Username": " gmalulu ",
" key_14 ": " gmalulu "
" Active ": " 1",
" key_15 ": " 1 "
}, {
" Id", " 2", " key_0 ", " 2", " Nom_Agent "
" Vasco ", " key_1 "
" Vasco ", " PNom_Agent "
" Kabangu "
" key_2 "
" Kabangu ", " Sexe_Agent ", " M ", " key_3 ", " M ", " Adresse_Agent "
" iSC ", " key_4 "
" iSC ", " Telephone_Agent ": " 243 ",
" key_5 ": " 243 ",
" Libelle_Role ": " Agency head ",
" key_6 ": " Agency head ",
" Email_Agent ": " vkabungu # ",
" key_7 ": " vkabungu # ",
" Nom_Agence ": " KIN 02 ",
" key_8 ": " KIN 02 ",
" Libelle_Caisse ": " ",
" key_9 ": " ",
" Id_Agence ",
" 2 ",
" key_10 ",
" 2 ",
" role_id ",
" 3 ",
" key_11 ",
" 3 ",
" Id_Caisse ": " ",
" key_12 ": " ",
" Password ": " 325a2cc052914ceeb8c19016c091d2ac ",
" key_13 ": " 325a2cc052914ceeb8c19016c091d2ac ",
" Username ": " vkabungu ",
" key_14 ": " vkabungu ",
" Active ": " 1 ",
" key_15 "
" 1 "
}, {
" Id", " 1", " key_0 ": " 1",
" Nom_Agent "
" Lepeya "
" key_1 "
" Lepeya "
" PNom_Agent "
" Otoko "
" key_2 "
" Otoko ",
" Sexe_Agent ",
" M ",
" key_3 ",
" M ",
" Adresse_Agent "
" lol122 Mombele "
" key_4 "
" lol122 Mombele "
" Telephone_Agent ": " 213 ",
" key_5 ": " 213 "
" Libelle_Role ": " Cashier ",
" key_6 ": " Cashier ",
" Email_Agent ": " lepeyaherve#agb.cd ",
" key_7 ": " lepeyaherve#agb.cd ",
" Nom_Agence ": " KIN 02 "
" key_8 ": " 02 KIN ",
" Libelle_Caisse "
" Case 01 ",
" key_9 "
" Case 01 ",
" Id_Agence ",
" 2",
" key_10 ",
" 2",
" role_id "
" 2"
" key_11 ": " 2",
" Id_Caisse ": " 2",
" key_12 ": " 2",
" Password ": " 325a2cc052914ceeb8c19016c091d2ac ",
" key_13 ": " 325a2cc052914ceeb8c19016c091d2ac ",
" Username": " lherve "
" key_14 ": " lherve ",
" Active ": " 0 ",
" key_15 ": " 0 "
}]
the script knockout:
<script type="text/javascript">
function Agent(data) {
this.id=data.id;
this.Nom_Agent=data.Nom_Agent;
this.PNom_Agent=data.PNom_Agent;
this.Sexe_Agent=data.Sexe_Agent;
this.Adresse_Agent=data.Adresse_Agent;
this.Telephone_Agent=data.Telephone_Agent;
this.Libelle_Role=data.Libelle_Role;
this.Email_Agent=data.Email_Agent;
this.Nom_Agence=data.Nom_Agence;
this.Username=data.Username;
this.Id_Role=data.Id_Role;
this.Id_Caisse=data.Id_Caisse;
this.Date_Affectation=data.Date_Affectation;
this.Libelle_Caisse=data.Libelle_Caisse;
this.Id_Lieu=data.Id_Lieu;
this.Active=ko.observable(data.Active)
}
function AgentListViewModel() {
// Data
var self = this;
self.agents= ko.observableArray([]);
$.ajax("/agence/allagent", {
contentType: "application/json",
success: function(result) {
var mappedAgents = $.map( result, function(item) {
return new Agent(item) ; });
self.agents(mappedAgents);
});
}
ko.applyBindings(new AgentListViewModel());
</script>
please help me
you can set the observable array using the knockout mapping plugin
AgentListViewModel function ( ) {
var self = this;
self.agents ko.observableArray = ( [ ] ) ;
$.ajax ("/branch/allagent" {
contentType : "application/json "
success : function ( result) {
ko.mapping.fromJS(result, {}, self.agents) };
}
});
}
I set up a fiddle with an example of how you can use it.
Example