Message Boards Message Boards

1
|
7089 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Replace sublist of a list

Here is a sample of the data I am trying to normalize, since the 6th column are in JSON format, I need to convert it using ImportString["str","JSON"], and then replace the original sample list.

 sample = dataClass1[[1 ;; 10]]

{{50112176, "datadrop:billing:usage:cloud:credits:instantapi:time", 
  "fa95220f-871c-4331-84ab-7951dd0666ca", 
  "4cb18a69-7b0d-464b-ad1a-b29fc826564e", 1437587924983, 
  "{\"queue\": \"message\", \"data\": {\"responseBytes\": 286, \
\"requestBytes\": 10, \"billableEvaluationId\": \
\"datadrop-cmp-prd7.wolframcloud.com:1437089155321:66748\", \
\"deploymentType\": \"API\", \"cputime\": 1.196, \"useruuid\": \"\", \
\"stage\": \"Deployment\", \"wallclocktime\": 1.101, \"owneruuid\": \
\"fa95220f-871c-4331-84ab-7951dd0666ca\", \"fileId\": \
\"4cb18a69-7b0d-464b-ad1a-b29fc826564e\"}, \"event\": \
\"datadrop:billing:usage:cloud:credits:instantapi:time\", \
\"identity\": \"datadrop-cmp-prd7.wolframcloud.com\", \"time\": \
1437587924983}", 
  "datadrop-cmp-prd7.wolframcloud.com:1437089155321:66748", "API", 
  "\\N"}, {50112174, 
  "datadrop:billing:usage:cloud:credits:instantapi:time", 
  "fa95220f-871c-4331-84ab-7951dd0666ca", 
  "31756465-be5e-472d-a638-a89b5e15d5ad", 1437587924134, 
  "{\"queue\": \"message\", \"data\": {\"responseBytes\": 148, \
\"requestBytes\": -1, \"billableEvaluationId\": \
\"datadrop-cmp-prd6.wolframcloud.com:1437089026575:65712\", \
\"deploymentType\": \"API\", \"cputime\": 0.073000000000000009, \
\"useruuid\": \"a18486f7-5c3e-495e-b96c-f35d60994ed8\", \"stage\": \
\"Deployment\", \"wallclocktime\": 0.083000000000000004, \
\"owneruuid\": \"fa95220f-871c-4331-84ab-7951dd0666ca\", \"fileId\": \
\"31756465-be5e-472d-a638-a89b5e15d5ad\"}, \"event\": \
\"datadrop:billing:usage:cloud:credits:instantapi:time\", \
\"identity\": \"datadrop-cmp-prd6.wolframcloud.com\", \"time\": \
1437587924134}", 
  "datadrop-cmp-prd6.wolframcloud.com:1437089026575:65712", "API", 
  "\\N"}, {50112173, 
  "datadrop:billing:usage:cloud:credits:instantapi:time", 
  "fa95220f-871c-4331-84ab-7951dd0666ca", 
  "4cb18a69-7b0d-464b-ad1a-b29fc826564e", 1437587923973, 
  "{\"queue\": \"message\", \"data\": {\"responseBytes\": 74, \
\"requestBytes\": 67, \"billableEvaluationId\": \
\"datadrop-cmp-prd6.wolframcloud.com:1437089026575:65711\", \
\"deploymentType\": \"API\", \"cputime\": 0.076999999999999999, \
\"useruuid\": \"\", \"stage\": \"Deployment\", \"wallclocktime\": \
0.096000000000000002, \"owneruuid\": \
\"fa95220f-871c-4331-84ab-7951dd0666ca\", \"fileId\": \
\"4cb18a69-7b0d-464b-ad1a-b29fc826564e\"}, \"event\": \
\"datadrop:billing:usage:cloud:credits:instantapi:time\", \
\"identity\": \"datadrop-cmp-prd6.wolframcloud.com\", \"time\": \
1437587923973}", 
  "datadrop-cmp-prd6.wolframcloud.com:1437089026575:65711", "API", 
  "\\N"}, {50112170, 
  "datadrop:billing:usage:cloud:credits:instantapi:time", 
  "fa95220f-871c-4331-84ab-7951dd0666ca", 
  "31756465-be5e-472d-a638-a89b5e15d5ad", 1437587922839, 
  "{\"queue\": \"message\", \"data\": {\"responseBytes\": 212, \
\"requestBytes\": -1, \"billableEvaluationId\": \
\"datadrop-cmp-prd4.wolframcloud.com:1437089109669:65996\", \
\"deploymentType\": \"API\", \"cputime\": 2.0, \"useruuid\": \
\"a18486f7-5c3e-495e-b96c-f35d60994ed8\", \"stage\": \"Deployment\", \
\"wallclocktime\": 2.0460000000000003, \"owneruuid\": \
\"fa95220f-871c-4331-84ab-7951dd0666ca\", \"fileId\": \
\"31756465-be5e-472d-a638-a89b5e15d5ad\"}, \"event\": \
\"datadrop:billing:usage:cloud:credits:instantapi:time\", \
\"identity\": \"datadrop-cmp-prd4.wolframcloud.com\", \"time\": \
1437587922839}", 
  "datadrop-cmp-prd4.wolframcloud.com:1437089109669:65996", "API", 
  "\\N"}, {50112169, 
  "datadrop:billing:usage:cloud:credits:instantapi:time", 
  "fa95220f-871c-4331-84ab-7951dd0666ca", 
  "4cb18a69-7b0d-464b-ad1a-b29fc826564e", 1437587921986, 
  "{\"queue\": \"message\", \"data\": {\"responseBytes\": 408, \
\"requestBytes\": 50, \"billableEvaluationId\": \
\"datadrop-cmp-prd7.wolframcloud.com:1437089155321:66747\", \
\"deploymentType\": \"API\", \"cputime\": 0.094, \"useruuid\": \"\", \
\"stage\": \"Deployment\", \"wallclocktime\": 0.129, \"owneruuid\": \
\"fa95220f-871c-4331-84ab-7951dd0666ca\", \"fileId\": \
\"4cb18a69-7b0d-464b-ad1a-b29fc826564e\"}, \"event\": \
\"datadrop:billing:usage:cloud:credits:instantapi:time\", \
\"identity\": \"datadrop-cmp-prd7.wolframcloud.com\", \"time\": \
1437587921986}", 
  "datadrop-cmp-prd7.wolframcloud.com:1437089155321:66747", "API", 
  "\\N"}, {50112168, 
  "datadrop:billing:usage:cloud:credits:instantapi:time", 
  "fa95220f-871c-4331-84ab-7951dd0666ca", 
  "25b3b6c2-0aca-4a18-a562-25c4a2313bbb", 1437587920698, 
  "{\"queue\": \"message\", \"data\": {\"responseBytes\": 210, \
\"requestBytes\": 393, \"billableEvaluationId\": \
\"datadrop-cmp-prd4.wolframcloud.com:1437089109669:65995\", \
\"deploymentType\": \"API\", \"cputime\": 0.184, \"useruuid\": \"\", \
\"stage\": \"Deployment\", \"wallclocktime\": 0.41700000000000004, \
\"owneruuid\": \"fa95220f-871c-4331-84ab-7951dd0666ca\", \"fileId\": \
\"25b3b6c2-0aca-4a18-a562-25c4a2313bbb\"}, \"event\": \
\"datadrop:billing:usage:cloud:credits:instantapi:time\", \
\"identity\": \"datadrop-cmp-prd4.wolframcloud.com\", \"time\": \
1437587920698}", 
  "datadrop-cmp-prd4.wolframcloud.com:1437089109669:65995", "API", 
  "\\N"}, {50112167, 
  "datadrop:billing:usage:cloud:credits:instantapi:time", 
  "fa95220f-871c-4331-84ab-7951dd0666ca", 
  "25b3b6c2-0aca-4a18-a562-25c4a2313bbb", 1437587920060, 
  "{\"queue\": \"message\", \"data\": {\"responseBytes\": 210, \
\"requestBytes\": 332, \"billableEvaluationId\": \
\"datadrop-cmp-prd3.wolframcloud.com:1437089089915:65923\", \
\"deploymentType\": \"API\", \"cputime\": 0.20600000000000002, \
\"useruuid\": \"\", \"stage\": \"Deployment\", \"wallclocktime\": \
0.436, \"owneruuid\": \"fa95220f-871c-4331-84ab-7951dd0666ca\", \
\"fileId\": \"25b3b6c2-0aca-4a18-a562-25c4a2313bbb\"}, \"event\": \
\"datadrop:billing:usage:cloud:credits:instantapi:time\", \
\"identity\": \"datadrop-cmp-prd3.wolframcloud.com\", \"time\": \
1437587920060}", 
  "datadrop-cmp-prd3.wolframcloud.com:1437089089915:65923", "API", 
  "\\N"}, {50112166, 
  "datadrop:billing:usage:cloud:credits:instantapi:time", 
  "fa95220f-871c-4331-84ab-7951dd0666ca", 
  "31756465-be5e-472d-a638-a89b5e15d5ad", 1437587919548, 
  "{\"queue\": \"message\", \"data\": {\"responseBytes\": 146, \
\"requestBytes\": -1, \"billableEvaluationId\": \
\"datadrop-cmp-prd3.wolframcloud.com:1437089089915:65922\", \
\"deploymentType\": \"API\", \"cputime\": 0.079000000000000001, \
\"useruuid\": \"a18486f7-5c3e-495e-b96c-f35d60994ed8\", \"stage\": \
\"Deployment\", \"wallclocktime\": 0.097000000000000003, \
\"owneruuid\": \"fa95220f-871c-4331-84ab-7951dd0666ca\", \"fileId\": \
\"31756465-be5e-472d-a638-a89b5e15d5ad\"}, \"event\": \
\"datadrop:billing:usage:cloud:credits:instantapi:time\", \
\"identity\": \"datadrop-cmp-prd3.wolframcloud.com\", \"time\": \
1437587919548}", 
  "datadrop-cmp-prd3.wolframcloud.com:1437089089915:65922", 
  "API", -1}, {50112164, 
  "datadrop:billing:usage:cloud:credits:instantapi:time", 
  "fa95220f-871c-4331-84ab-7951dd0666ca", 
  "25b3b6c2-0aca-4a18-a562-25c4a2313bbb", 1437587919269, 
  "{\"queue\": \"message\", \"data\": {\"responseBytes\": 210, \
\"requestBytes\": 421, \"billableEvaluationId\": \
\"datadrop-cmp-prd2.wolframcloud.com:1437089077345:65730\", \
\"deploymentType\": \"API\", \"cputime\": 0.65500000000000003, \
\"useruuid\": \"\", \"stage\": \"Deployment\", \"wallclocktime\": \
0.44700000000000001, \"owneruuid\": \
\"fa95220f-871c-4331-84ab-7951dd0666ca\", \"fileId\": \
\"25b3b6c2-0aca-4a18-a562-25c4a2313bbb\"}, \"event\": \
\"datadrop:billing:usage:cloud:credits:instantapi:time\", \
\"identity\": \"datadrop-cmp-prd2.wolframcloud.com\", \"time\": \
1437587919269}", 
  "datadrop-cmp-prd2.wolframcloud.com:1437089077345:65730", 
  "API", -7}, {50112162, 
  "datadrop:billing:usage:cloud:credits:instantapi:time", 
  "fa95220f-871c-4331-84ab-7951dd0666ca", 
  "4cb18a69-7b0d-464b-ad1a-b29fc826564e", 1437587919192, 
  "{\"queue\": \"message\", \"data\": {\"responseBytes\": 835, \
\"requestBytes\": -1, \"billableEvaluationId\": \
\"datadrop-cmp-prd2.wolframcloud.com:1437089077345:65731\", \
\"deploymentType\": \"API\", \"cputime\": 0.094, \"useruuid\": \"\", \
\"stage\": \"Deployment\", \"wallclocktime\": 0.12300000000000001, \
\"owneruuid\": \"fa95220f-871c-4331-84ab-7951dd0666ca\", \"fileId\": \
\"4cb18a69-7b0d-464b-ad1a-b29fc826564e\"}, \"event\": \
\"datadrop:billing:usage:cloud:credits:instantapi:time\", \
\"identity\": \"datadrop-cmp-prd2.wolframcloud.com\", \"time\": \
1437587919192}", 
  "datadrop-cmp-prd2.wolframcloud.com:1437089077345:65731", 
  "API", -1}}

While line 1 works, line 2 doesn't work, why is this?

sample /. Table[sample[[i, 6]] -> ImportString[sample[[i, 6]], "JSON"], {i, 1, 
   10}]
sample /. sample[[All, 6]] -> ImportString[#, "JSON"] & /@ 
 sample[[All, 6]]
POSTED BY: Wenzhen Zhu
4 Replies
Posted 10 years ago

And another one:

sample /. Map[sample[[#, 6]] -> ImportString[sample[[#, 6]], "JSON"] &, Range@Length@sample]

This will not change the structure. Which your third example does:

enter image description here

enter image description here

POSTED BY: Hans Milton
Posted 10 years ago

Deleted

POSTED BY: Hans Milton
Posted 10 years ago

One alternative:

test = sample;
(test[[#, 6]] = ImportString[sample[[#, 6]], "JSON"]) & /@ Range[Length@sample];
test
POSTED BY: Hans Milton

Is there a way better than

sample /. sample[[#, 6]] -> ImportString[sample[[#, 6]], "JSON"] & /@ 
 Range[Length[sample]]
POSTED BY: Wenzhen Zhu
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract